Как передать коллекцию элементов в модель Views? - PullRequest
0 голосов
/ 23 февраля 2012

Я использую MVC 3. Я изменил стандартную RegisterModel, которая используется при регистрации нового пользователя, добавив свойство ICollection типа OwnerApartments. Пользователь, который регистрируется, может добавить одну или несколько квартир в коллекцию. На мой взгляд, квартиры добавляются через модальную форму в HTML-таблицу в представлении. Мой вопрос: как мне добавить Квартиры (хранящиеся в таблице html), когда я отправляю данные формы обратно контроллеру.

   public class RegisterModel
{
    [Required]
    [Display(Name = "User Name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email Address")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    [Required]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Required]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    [Required(ErrorMessage = "At least one Apartment must be added.")]
    public virtual ICollection<OwnerApartment> OwnerApartments { get; set; }
}

@model TestForum.Models.RegisterModel
@{
ViewBag.Title = "Register Account";
}

<script src="../../Scripts/add-apartment-modal.js" type="text/javascript"></script>

<h2>
Create a New Account</h2>
<p>
Use the form below to create a new account.
</p>
<p>
Passwords are required to be a minimum of @Membership.MinRequiredPasswordLength
characters in length.
</p>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct      the errors and try again.")
<div>
    <fieldset>
        <legend>Account Information</legend>
        <div class="editor-label">
            @Html.LabelFor(m => m.UserName)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.UserName)
            <br />
            @Html.ValidationMessageFor(m => m.UserName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(m => m.Email)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.Email)
            <br />
            @Html.ValidationMessageFor(m => m.Email)
        </div>
        <div class="editor-label">
            @Html.LabelFor(m => m.Password)
        </div>
        <div class="editor-field">
            @Html.PasswordFor(m => m.Password)
            <br />
            @Html.ValidationMessageFor(m => m.Password)
        </div>
        <div class="editor-label">
            @Html.LabelFor(m => m.ConfirmPassword)
        </div>
        <div class="editor-field">
            @Html.PasswordFor(m => m.ConfirmPassword)
            <br />
            @Html.ValidationMessageFor(m => m.ConfirmPassword)
        </div>
        <div class="editor-label">
            @Html.LabelFor(m => m.FirstName)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.FirstName)
            <br />
            @Html.ValidationMessageFor(m => m.FirstName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(m => m.LastName)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.LastName)
            <br />
            @Html.ValidationMessageFor(m => m.LastName)
        </div>
    </fieldset>
    <div>
        <div id="dialog-form" title="Add your apartment(s)">
            <p class="validateTips">
                All form fields are required.</p>
            <table>
                <tr>
                    <td>
                        <label for="complex">
                            Your Complex:</label>
                    </td>
                    <td>
                        <input type="text" name="complexname" id="complexname"     class="text ui-widget-content ui-corner-all" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="apartmentno">
                            Apartment No:</label>
                    </td>
                    <td>
                        <input type="text" name="apartmentno" id="apartmentno"     value="" class="text ui-widget-content ui-corner-all" />
                    </td>
                </tr>
            </table>
        </div>
        <fieldset>
            <legend>Apartment Information</legend>
            <div id="apartment-contain" class="ui-widget">
                <table id="complexes-table" class="ui-widget ui-widget-content">
                    <thead>
                        <tr class="ui-widget-header">
                            <th>
                                Complex Name
                            </th>
                            <th>
                                Apartment No.
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>
                            </td>
                            <td>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <div class="editor-field">
                    @Html.ValidationMessageFor(m => m.OwnerApartments)
                </div>
            </div>
            <div style="margin-top: 10px">
                <button id="add-apartment" name="add-apartment">
                    Add your Apartment(s)</button>
            </div>
        </fieldset>
    </div>
    <p>
        <input type="submit" id="register" value="Register" />
    </p>
</div>

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...