部分ビューでリストボックスをバインドしようとしていますが、例外が発生します...
オブジェクト参照がオブジェクト インスタンスに設定されていません。
参照@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.cshtmlUsers.cshtmlを参照しているビューは次のとおりです。
@model IEnumerable<RobotDog.Models.UserModel>
<table></table>
<div id="addUser">
@Html.Partial("_AddUser", new ViewDataDictionary())
</div>