2013-01-12 143 views
0

嗨,我是新来的MVC 3只是一个初学者。我试图在视图中创建一个新的下拉框,但我得到错误说''System.Web.Mvc.HtmlHelper'不包含'DropDownListFor'的定义和最好的扩展方法重载'System.Web。 Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper,System.Linq.Expressions.Expression>,System.Collections.Generic.IEnumerable)'有一些无效参数“。MVC 3.下拉框

这里是查看代码

<tr> 
    <td> 
     <label> 
     Customer Name 
     </label> 
    </td> 
    <td> 
    @Html.DropDownListFor(A => A.Roles, Model.Roles); 
    </td> 
</tr> 

控制器代码

public ActionResult Index() 
     { 
      var Model = new Customer(); 
      Model.Roles = getRoles(); 

      return View(Model); 
     } 

     private List<string> getRoles() 
     { 
      List<string> roles = new List<string> 
      { 
       "Developer", 
       "Tester", 
       "Project Manager", 
       "Team Lead", 
       "QA" 
      }; 
      return roles; 
     } 

回答

0

Firdt我建议你创建一个视图模型类视图:

public class IndexViewModel 
{ 
    public IList<string> Roles { get; set; } 

    public string SelectedRole { get; set; } 
} 

然后调用像这样的看法:

public ActionResult Index() 
{ 
    List<string> roles = new List<string> 
    { 
     "Developer", 
     "Tester", 
     "Project Manager", 
     "Team Lead", 
     "QA" 
    }; 

    var viewModel = new IndexViewModel(); 

    viewModel.Roles = roles; 

    return this.View(viewModel); 
} 

于是最后,呈现下拉列表:

@model Mvc4.Controllers.IndexViewModel 

@Html.DropDownListFor(model => model.SelectedRole, new SelectList(Model.Roles)) 

你需要存储所选择的项目(SelectedRole)一个变量,你需要用角色的选择为SelectList,因为下拉助手不能用用于第二个参数的IEnumerable

+0

感谢它的工作.. –