1

我有一个模型如下:

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吗?

4

1 回答 1

1

你应该添加一个DepartmentIdDepartmentsList你的RegisterModel

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();
    }
}
于 2013-07-21T14:02:51.833 回答