我有一個模型,它看起來是這樣的:如何在MVC 3 Razor中將模型綁定到模型的屬性?
public class EditUserViewModel
{
public EditUserViewModel()
{
}
public EditUserDataModel User { get; set; }
}
隨着支持對象,看起來像這樣:
public class EditUserDataModel
{
public EditUserDataModel()
{
Roles = new List<UserRoleListDataModel>();
}
[DisplayName("First Name")]
public string FirstName { get; set; }
[DisplayName("Last Name")]
public string LastName { get; set; }
[DisplayName("Full Name")]
public string FullName { get { return FirstName + " " + LastName; } }
public List<UserRoleListDataModel> Roles { get; set; }
}
而且UserRoleListDataModel看起來是這樣的:
public class UserRoleListDataModel
{
public Guid Id { get; set; }
public string RoleName { get; set; }
public bool UserIsInRole { get; set; }
}
然後,在我的Razor文件中,我使用瞭如下所示的全部內容:
@foreach (var role in Model.User.Roles)
{
<tr>
<td>@role.RoleName</td>
<td>@Html.CheckBoxFor(x=>role.UserIsInRole)</td>
</tr>
}
我遇到的問題是,當我提交表單並點擊我的控制器操作時,角色列表未填充到我的新模型上。
這裏是控制器上的提交操作是這樣的:
public ActionResult EditUser(EditUserViewModel model) // model.User.Roles is empty.
{
// Do some stuff...
return RedirectToAction("UserList");
}
任何人有什麼建議?
https://github.com/danludwig/BeginCollectionItem&http://nuget.org/packages/BeginCollectionItem – danludwig 2012-03-16 19:16:57