已经这么久了!关于以下问题的任何建议......
已添加代码:
型号:AccountModels.cs
[Required]
[Display(Name = "Select role: ")]
public String Role { get; set; }
Controller: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>)
代码可以运行并允许用户选择一个角色,但如果用户在创建新帐户时违反了验证规则,则会崩溃。
示例:如果密码不匹配,则验证会启动,但由于添加了新代码,应用程序才会崩溃。
创建错误的代码:
@Html.DropDownListFor(m => m.Role, ViewBag.roleList as IEnumerable<SelectListItem>)
错误代码
具有键“Role”的ViewData项的类型为“System.String”,但必须是“IEnumerable”类型。
答案 0 :(得分:3)
我猜你在你的验证失败的HttpPost操作方法中遇到了这个问题。我相信你的HttpPost行动方法中有return View()
陈述。再次返回View时,ViewData为null。因此,您需要再次为下拉列表重新加载数据
public ActionResult Register()
{
//If validation ok, save
if (createStatus == MembershipCreateStatus.Success)
{
//dio stuff
}
else
{
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();
}
}
理想情况下,我不会使用ViewData来处理这个问题。我将使用我的ViewModel来处理这个问题。
public class RegisterViewModel
{
public string FirstName { set;get;}
public IEnumerable<SelectListItem> Roles { get; set; }
public int SelectedRoleId { get; set; }
}
并在您的get动作调用中,将ViewModel传递回View。
public ActionResult Register()
{
RegisterViewModel objViewModel=new RegisterViewModel();
objViewModel.Roles=GetAllRolesAs(); // returns the list of roles to this property
return View(objViewModel);
}
使您的视图成为强类型
@model RegisterViewModel
@using (Html.BeginForm())
{
@Html.TextBoxFor(m => m.FirstName)
@Html.DropDownListFor(x => x.SelectedRoleId,new SelectList(Model.Roles , "Value","Text"), "Select Role")
<input type="submit" value="Save" />
}
并在您的HttpPostAction中,接收此ViewModel对象作为参数
[HttpPost]
public ActionResult Register(RegisterViewModel objVM)
{
if(ModelState.IsValid)
{
//Check your custom validation also
if (createStatus == MembershipCreateStatus.Success)
{
//Save and then Redirect
}
else
{
objVM.Roles=GetAllRolesAs(); // get the list of roles here again because HTTP is stateless
}
}
return View(objVM);
}