我试图在我的部分视图中绑定一个 ListBox,但我得到了异常......
你调用的对象是空的。
参考@Html.ListBoxFor(x => x.SelectedRoles, Model.Roles)
我不确定我做错了什么......
模型:
public class RegisterModel {
public string UserName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
public string[] SelectedRoles { get; set; }
public MultiSelectList Roles { get; set; }
}
控制器:
public class AdminController : Controller {
[ChildActionOnly]
public ActionResult _AddUser() {
var model = new RegisterModel {
Roles = new MultiSelectList(Roles.GetAllRoles())
};
return View(model);
}
}
PartialView _AddUser.cshtml:
@model RobotDog.Models.RegisterModel
@using(Html.BeginForm("_AddUser","Admin", FormMethod.Post)) {
@Html.EditorFor(x => x.Email, new { @class = "input-xlarge", @placeholder = "Email"})
@Html.EditorFor(x => x.UserName, new { @class = "input-xlarge", @placeholder = "User Name"})
@Html.EditorFor(x => x.Password, new { @class = "input-xlarge", @placeholder = "Password"})
@Html.ListBoxFor(x => x.SelectedRoles, Model.Roles)
}
这是引用 _AddUser.cshtml Users.cshtml 的视图:
@model IEnumerable<RobotDog.Models.UserModel>
<table></table>
<div id="addUser">
@Html.Partial("_AddUser", new ViewDataDictionary())
</div>