2010-07-02 79 views
1

CS1928:'System.Web.Mvc.HtmlHelper'不包含'DropDownList'的定義和最佳擴展方法重載'System.Web.Mvc.Html.SelectExtensions.DropDownList( System.Web.Mvc.HtmlHelper,string,string)'有一些無效的參數asp.net mvc html.dropdownlist error

我從用戶角色形成memebership提供商,然後要求用戶選擇角色。角色位於註冊頁面的下拉列表中。

public ActionResult ChangePassword() 
     { 

      ViewData["PasswordLength"] = MembershipService.MinPasswordLength; 
      var roles = Roles.GetAllRoles().ToList();  
      ViewData["Roles"] = new SelectList(roles); 

      return View(); 
     } 

<p> 
       <%= Html.DropDownList("Userrole",((SelectList)ViewData["Roles"]).Items,"--Select One---") %> 
       </p> 

回答

1

嘗試

<%= Html.DropDownList("Userrole",((SelectList)ViewData["Roles"]),"--Select One---") %> 

更新

有別的東西,導致了問題(你有沒有爲DropDownList其他擴展?)

以下作品me:

動作

public ActionResult About() 
     { 
      var x = new List<string> 
      { 
       "A", "B", "C" 
      }; 
      var y = new SelectList(x); 
      ViewData["z"] = y; 
      return View(); 
     } 

查看

<%= Html.DropDownList("Userrole",((SelectList)ViewData["z"]),"--Select One---") %> 
+0

它給我這個錯誤,當我沒有那個 異常詳細信息:System.InvalidOperationException:沒有類型爲'IEnumerable '的ViewData項具有鍵'Userrole'。 – Pinu 2010-07-02 15:56:15

+0

請參閱上面的更新。 – 2010-07-02 16:09:26

+0

我複製粘貼並運行相同的代碼,你發送給我,它仍然會引發上述錯誤。 – Pinu 2010-07-02 16:24:37

0

控制器代碼

public ActionResult About() 
    { 
     var x = new [] 
     { 
      "A", "B", "C" 
     }; 
     var y = new SelectList(x); 
     ViewData["z"] = y; 
     return View(); 
    } 

這是你的視圖代碼 你試試這個它是工作的罰款

<%: Html.DropDownList("Userrole", ViewData["z"] as SelectList,"--selectone--")%>