2013-10-01 27 views
1

我得到这个错误“CS1928:DropDownListFor ASP MVC4

'System.Web.Mvc.HtmlHelper' 并不 不包含 'DropDownListFor' 和最佳推广 方法重载 “系统的定义。 Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, System.Collections.Generic.IEnumerable)” 有一些无效参数”

我的控制器看起来像这样

public class IndexController : Controller 
{ 
     public ActionResult Index() 
     { 
       EpfeSelectScreen model = new EpfeSelectScreen(); 

       var b = (from a in dbEntitiesErste.CONFIG_APPLICATIONS 
           orderby a.APPLICATION_ID 
           select new EPFE.CustomDataObjects.CustomObjects 
           { 
            Text = a.APPLICATION_NAME, 
            Value = a.APPLICATION_ID 
           }); 

       model.Application = b.OrderBy(x => x.Text).ToList(); 

       return View(model); 




     } 
} 

我的模型是这样的

public class EpfeSelectScreen 
{ 
    public string Search { get; set; } 
    public string selectedApplication { get; set; } 
    public List<SelectListItem> Country { get; set; } 
    public List<CustomObjects> Application { get; set; } 
    public List<SelectListItem> MetaData { get; set; } 
    public List<SelectListItem> References { get; set; } 
    public List<SelectListItem> ReferencedBy { get; set; } 
    public List<SelectListItem> TreeView { get; set; }  

    public EpfeSelectScreen() 
    { 
     Country = new List<SelectListItem>(); 
     Application = new List<CustomObjects>();   
     References = new List<SelectListItem>(); 
     ReferencedBy = new List<SelectListItem>(); 
     TreeView = new List<SelectListItem>();   
    } 
} 

我CustomObjects是这个

public class CustomObjects 
{ 
    public string Text { get; set; } 
    public short Value { get; set; } 
} 

我在我的model.Application一个记录,但是当数据传递我的观点,我得到这个错误。

而且我查看看起来像这样

@model EPFE.Controllers.EpfeSelectScreen 
@Html.DropDownListFor(m => m.selectedApplication, Model.Application) 

如何解决这个问题呢?我究竟做错了什么?我尝试使用ListBoxFor时遇到同样的错误。

回答

5

DropDownListFor需要SelectList作为第二个参数,而您通过List

你可能有类似以下内容:

@Html.DropDownListFor(m => m.selectedApplication, 
        new SelectList(Model.Application,"Value","Text")) 
+0

它工作。谢谢。我不知道与SelectList和List的那部分。你帮了我很多。 – user2718165

0

添加条目砸下来,你可以使用的ViewData传递值下拉。

控制器

: 你可以有场景像

 IList<SelectListItem> SelectedOptions = new List<SelectListItem>(); 
     IList<Options> DropDownOptions = new List<Options>(); 
     //Add items to this shipping 
     foreach (var item in DropDownOptions) 
     { 
      SelectListItem sliItem = new SelectListItem(); 
      string optionText = item._name; 
      sliItem.Value = item._value.ToString(); 
      sliItem.Text = optionText; 
      SelectedOptions.Add(sliItem); 
     } 
     ViewData["Option"] = SelectedOptions; 

,并在视图中,您可以简单地使用

@Html.DropDownList("Option", ViewData["Option"] as SelectList) 

提交表格时,如果您使用的是类,千万记得要将字符串属性与名称选项放在类中,以便您可以在提交表单时在控制器中使用它的选定值。