0
我想一個列表框在我的局部視圖綁定,但我發現了異常的實例...MVC:MultiSelectList對象引用未設置到對象
對象引用不設置到對象的實例。
引用@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>
一個問題,這似乎是撕裂佈局內部分的部分。換句話說,partialView被封裝在'
' – bflemi3@ bflemi3 - 您在主視圖內使用另一個html,head和body獲得渲染視圖?你使用Html.Action還是Html.RenderAction? – Carth
我正在使用Html.Action – bflemi3