在此去過太久!以下問題的任何建議...加入爲什麼DropDownListFor助手在驗證錯誤後失敗?
代碼:
型號:AccountModels.cs
[Required]
[Display(Name = "Select role: ")]
public String Role { get; set; }
控制器:AccountController.cs
// GET: /Account/Register
public ActionResult Register()
{
List<SelectListItem> list = new List<SelectListItem>();
SelectListItem item;
foreach (String role in Roles.GetAllRoles())
{
item = new SelectListItem { Text = role, Value = role };
list.Add(item);
}
ViewBag.roleList = (IEnumerable<SelectListItem>)list;
return View();
}
此外,在[HTTpPost]
if (createStatus == MembershipCreateStatus.Success)
{
Roles.AddUserToRole(model.UserName, model.Role);
MailClient.SendWelcome(model.Email);
FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
return RedirectToAction("Create", "Customers");
}
查看:Register.cshtml
<div class="editor-label">
@Html.LabelFor(m => m.Role)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => m.Role, ViewBag.roleList as IEnumerable<SelectListItem>)
代碼工作,並允許用戶選擇一個角色,但如果除了創建一個新帳戶,當用戶中斷驗證規則下降。
例如:如果密碼不匹配通常會驗證在但因踢添加了新的代碼就能應用崩潰的。
它創建的錯誤代碼:我想你有這個問題,在您的驗證失敗,您的HttpPost操作方法時存在的
@Html.DropDownListFor(m => m.Role, ViewBag.roleList as IEnumerable<SelectListItem>)
錯誤代碼
The ViewData item that has the key 'Role' is of type 'System.String' but must be of type 'IEnumerable'.
在你httppost發生這個錯誤時驗證失敗? – Shyju 2012-04-08 02:30:49