2012-01-05 105 views
0

我使用MVC3剃刀和我想打一個登記表,我可以選擇用下拉列表的作用。登記表:ViewData的產品類型的「system.sting」必應「的IEnumerable <selectlistitem>

我得到的角色在下拉列表和所有的,但是當我點擊提交我得到:

具有關鍵的角色「爲System.String類型,但必須是IEnumerable<SelectListItem>類型的ViewData項目

我的代碼

型號:

[Required] 
    [Display(Name = "Select role: ")] 
    public String Role { get; set; } 

我的控制器:

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] 
    public ActionResult Register(RegisterModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      // 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) 
      { 
       Roles.AddUserToRole(model.UserName, model.Role); 
       return RedirectToAction("Index", "Home"); 
      } 
      else 
      { 
       ModelState.AddModelError("", ErrorCodeToString(createStatus)); 
      } 
     } 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 

我的觀點:

<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.ValidationMessageFor(m => m.Role) 

     </div> 

當我使用調試器來檢查我的碼,在控制器中,當我查看參數模型並在model.Role處看到右邊的字符串。 有誰知道我的問題的任何解決方案?

+0

我只是嘗試這樣的代碼(有一些修改,以取代常規GetAllRoles,添加提交按鈕的形式和以去除後的隸屬線)和它工作得很好。最後,你是否說這是在調試模式下工作,但不是在發佈時運行它? – Dangerous 2012-01-05 14:24:06

+0

不,它不工作在調試模式,但模型獲取正確的字符串,但當我做model.Role它,我得到的錯誤信息。 – TobiasW 2012-01-05 15:20:47

+0

那麼你是說你可以運行代碼,它會在註冊視圖中顯示下拉框?從我收集的內容中,您可以在下拉框中選擇一個值,然後在您的發佈操作開始處點擊提交併中斷。你可以看到模型包含選定的值?那麼如果你在這個休息之後繼續執行代碼,那麼錯誤會發生?它是否正確? – Dangerous 2012-01-05 15:47:37

回答

3

在您的文章的行動,你不再植中的情況下ViewBagroleList,當你重新顯示視圖:

... 
// If we got this far, something failed, redisplay form 

// but before redisplaying the form ensure that we have populated the 
// ViewBag.roleList that this form depends on 
ViewBag.roleList = ... same stuff as your GET action 
return View(model); 

這是說,我會建議你擺脫ViewBag並添加RoleList財產您的視圖模型:

public class RegisterModel 
{ 
    [Required] 
    [Display(Name = "Select role: ")] 
    public string Role { get; set; } 

    public IEnumerable<SelectListItem> RoleList 
    { 
     get 
     { 
      return Roles 
       .GetAllRoles() 
       .Select(x => new SelectListItem 
       { 
        Value = x, 
        Text = x 
       }) 
       .ToList(); 
     } 
    } 
} 

然後:

public ActionResult Register() 
{ 
    var model = new RegisterModel(); 
    return View(model); 
} 

[HttpPost] 
public ActionResult Register(RegisterModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     // 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) 
     { 
      Roles.AddUserToRole(model.UserName, model.Role); 
      return RedirectToAction("Index", "Home"); 
     } 
     else 
     { 
      ModelState.AddModelError("", ErrorCodeToString(createStatus)); 
     } 
    } 

    // If we got this far, something failed, redisplay form 
    return View(model); 
} 

和強類型視圖:

@model RegisterModel 
... 

<div class="editor-label"> 
    @Html.LabelFor(m => m.Role) 
</div> 
<div class="editor-field"> 
    @Html.DropDownListFor(m => m.Role, Model.RoleList) 
    @Html.ValidationMessageFor(m => m.Role) 
</div> 

沒有更多ViewBag =>沒有問題。

+1

感謝幫助,但我不得不使用foreach填充RoleList,.select給了我一個錯誤。 – TobiasW 2012-01-06 14:23:51

+0

儘管我落後了4年....我實現了上述代碼,並且出現錯誤「無法連接到數據庫」 – singhswat 2016-09-09 23:04:20

相關問題