如何将项目集合传递给Views模型?

时间:2012-02-23 18:19:36

标签: jquery asp.net-mvc-3

我正在使用MVC 3.我修改了注册新用户时使用的标准RegisterModel,方法是添加类型为OwnerApartments的ICollection属性。正在注册的用户可以将一个或多个公寓添加到集合中。在我的视图中,公寓通过模态窗体添加到视图中的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>

}

0 个答案:

没有答案