2012-04-08 45 views
0

在此去過太久!以下問題的任何建議...加入爲什麼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'.

+0

在你httppost發生這個錯誤時驗證失敗? – Shyju 2012-04-08 02:30:49

回答

3

。我相信你在你的HttpPost操作方法中有return View()聲明。當您再次返回視圖時,您的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,接收此視圖模型對象作爲參數

[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); 
} 
+0

試過你的解決方案,但驗證失敗時出現同樣的錯誤? – 2012-04-08 03:32:14

+0

@DazaAza:你在哪裏得到的錯誤,當您再次加載看法?使用在VisualStudio的破發點,以確保您返回的數據具有正確的價值 – Shyju 2012-04-08 03:34:06

+0

@DazaAza:我更新了我的答案,使用強類型的意見。 – Shyju 2012-04-08 03:48:17