与子模型的模型绑定

时间:2013-07-20 11:21:27

标签: c# asp.net-mvc forms model-binding

我的模型如下:

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

    [Required]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    public virtual Department Department { get; set; }

}

以下是控制器操作:

    [AllowAnonymous]
    public ActionResult Register(int departmentId)
    {
        using (var db = new DataContext())
        {
            var department = db.Departments.Find(departmentId);

            var registerModel = new RegisterModel {Department = department};

            return View(registerModel);
        } 
    }

我有部门列表页面。比如Foo,那么我想把用户分到Foo。

1 . Foo - add user

然后我有一个注册视图。

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()
    @Html.HiddenFor(x => x.Department.Id)


    <fieldset>
        <legend>Registration Form</legend>
        <ol>
            <li>
                @Html.LabelFor(m => m.UserName)
                @Html.TextBoxFor(m => m.UserName)
            </li>
            <li>
                @Html.LabelFor(m => m.Password)
                @Html.PasswordFor(m => m.Password)
            </li>

        </ol>
        <input type="submit" value="Register" />
    </fieldset>
}

现在我的问题是如何将部门绑定到模型?与部门ID?或部门对象?

我可以访问部门,我从数据库填充,然后我需要将其传递给View。但我怎么通过呢?所以在注册提交后,我可以访问它还是ID?

1 个答案:

答案 0 :(得分:1)

您应该在DepartmentId添加DepartmentsListRegisterModel

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

    [Required]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    public int DepartmentId { get; set; }   

    // You don't really need this.
    //public virtual Department Department { get; set; }

    // Create this list in your controller before sending it to the view.
    public IEnumerable<SelectListItem> DepartmentsList { get; set; }
}

DepartmentsList操作中填充Register

public ActionResult Register()
{
    using (var db = new DataContext())
    {
        var departments = db.Departments.Select(department => new SelectListItem 
                                                                    { 
                                                                        Value = department.Id, 
                                                                        Text = department.Name
                                                                    }).ToList();

        var registerModel = new RegisterModel { DepartmentsList = departments };
        return View(registerModel);
    }
}

然后在视图中创建一个下拉列表:

<li>
    @Html.LabelFor(m => m.Department)
    @Html.DropDownListFor(m => m.DepartmentId, Model.DepartmentsList)
</li>

然后在HttpPost行动中,您接受了RegisterModel

[HttpPost]
public ActionResult Register(RegisterModel registerModel)
{
    using (var db = new DataContext())
    {
        // Note: this is just an example..
        var user = new User
        {
            UserName = registerModel.UserName,
            Password = registerModel.Password,
            DepartmentId = registerModel.DepartmentId
        }

        db.Users.Add(user);
        db.SaveChanges();
    }
}