0

搞这个太久了!关于以下问题的任何建议...

添加的代码:

模型: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>)

该代码有效并允许用户选择一个角色,但如果用户在创建新帐户时违反验证规则,该代码就会崩溃。

示例:如果密码不正常匹配,验证将启动,但由于添加了新代码,应用程序就会崩溃。

创建错误的代码:

@Html.DropDownListFor(m => m.Role, ViewBag.roleList as IEnumerable<SelectListItem>)

错误代码

具有键“角色”的 ViewData 项的类型为“System.String”,但必须为“IEnumerable”类型。

4

1 回答 1

3

我猜您在验证失败的 HttpPost 操作方法中会出现此问题。我相信您return View()在 HttpPost 操作方法中有声明。当您再次返回视图时,您的 ViewData 为空。因此,您需要再次为下拉列表重新加载数据

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; }
}

并在您的获取操作调用中,将 ViewModel 传递回您的视图。

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);
}
于 2012-04-08T02:37:27.747 回答