DropDownListFor ASP MVC4

use*_*165 1 c# razor asp.net-mvc-4

我收到此错误"CS1928:

'System.Web.Mvc.HtmlHelper'不包含'DropDownListFor'的定义和最佳扩展方法重载'System.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); 




       }
}
Run Code Online (Sandbox Code Playgroud)

我的模特是这样的

    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>();        
    }
}
Run Code Online (Sandbox Code Playgroud)

我的CustomObjects就是这个

    public class CustomObjects
{
    public string Text { get; set; }
    public short Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的model.Application中有一条记录,但是当该数据传递给我的View时,我得到了这个错误.

我的视图看起来像这样

@model EPFE.Controllers.EpfeSelectScreen
@Html.DropDownListFor(m => m.selectedApplication, Model.Application) 
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题呢?我究竟做错了什么?当我尝试使用ListBoxFor时,我得到相同的错误.

Gia*_*los 5

DropDownListFor需要SelectList作为第二个参数,而你传递List.

您可以拥有以下内容:

@Html.DropDownListFor(m => m.selectedApplication, 
                     new SelectList(Model.Application,"Value","Text"))
Run Code Online (Sandbox Code Playgroud)