我有一個管理員網站爲管理員創建用戶。在這裏他必須爲用戶選擇角色 - 比如Asp.NET配置站點。我做了3個不同角色的複選框。在Asp.net MVC3中的多個角色
[Authorize(Roles = "Admin")]
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
var Rolemodel = model.RolesContainer;
// Attempt to register the user
MembershipCreateStatus createStatus;
Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", ErrorCodeToString(createStatus));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
[Authorize(Roles = "Admin")]
public ActionResult Register()
{
List<SelectListItem> tempRoles = new List<SelectListItem>();
tempRoles.Add(new SelectListItem{ Text = "Admin", Selected = false, Value = "Admin" });
tempRoles.Add(new SelectListItem{ Text = "Production", Selected = false, Value = "Production"});
tempRoles.Add(new SelectListItem{ Text = "Sale", Selected = false, Value = "Sale"});
return View(new RegisterModel { RolesContainer = tempRoles });
}
----查看--------
@{ foreach (var item in Model.RolesContainer)
{
@Html.DisplayFor(m => item.Text)
@Html.CheckBoxFor(m => item.Selected)
}
}
當我檢查這些問題並提交,我可以在我的Register
行動斷點,但RolesContainer
是空的這一點 - 任何人都可以告訴我這是爲什麼?
你可以顯示你的httpost方法的簽名嗎?我想你可能會期待一個類型參數不同於你傳入視圖的模型。 – Nope 2012-04-21 20:05:34
我更新現在的代碼,所述點是當我到: 如果(ModelState.IsValid) { VAR =的RoleModel model.RolesContainer; } RolesContainer爲空,且不包含所選值 – Mrlondon7100 2012-04-21 21:52:36