Model view controller Html.DropDownListFor-在基础模型上枚举时填充与模型属性对应的列表?

Model view controller Html.DropDownListFor-在基础模型上枚举时填充与模型属性对应的列表?,model-view-controller,drop-down-menu,model,enumeration,Model View Controller,Drop Down Menu,Model,Enumeration,我试图在视图中填充一个下拉列表,这导致我: 解决方案很有效。现在我有了一个“创建新项目”视图,它接受了一个“项目”模型,我的模型包括: public int projectPrimaryEmployeeID { get { return _projectPrimaryEmployeeID; } set { _projectPrimaryEmployeeID = value; }

我试图在视图中填充一个下拉列表,这导致我:

解决方案很有效。现在我有了一个“创建新项目”视图,它接受了一个“项目”模型,我的模型包括:

 public int projectPrimaryEmployeeID
    {
        get
        { return _projectPrimaryEmployeeID; }
        set
        {
            _projectPrimaryEmployeeID = value;
        }
    }

    public IEnumerable<SelectListItem> employeeList { get; set; }
但我有一个错误:

'System.Collections.Generic.IEnumerable<MVCCodeProject.Models.project>' does not contain a definition for 'employeeList' and no extension method 'employeeList' accepting a first argument of type 'System.Collections.Generic.IEnumerable<MVCCodeProject.Models.project>' could be found (are you missing a using directive or an assembly reference?)
“System.Collections.Generic.IEnumerable”不包含“employeeList”的定义,并且找不到接受“System.Collections.Generic.IEnumerable”类型的第一个参数的扩展方法“employeeList”(是否缺少using指令或程序集引用?)

因此,在对模型进行枚举时,显示下拉列表的路径可能有点不同,是否有帮助?

因此,首先需要将模型更改为IList:

@model IList<MVCCodeProject.Models.project>
@model-IList
然后,您将能够迭代列表,并使用索引绑定到该列表中的每个项目:

@for (int i = 0; i < Model.Count(); i++)
{
    Html.DropDownListFor(proj => proj[i], new SelectList(proj.employeeList, "Value", "Text"));
}
for(int i=0;iproj[i],新的选择列表(proj.employeeList,“Value”,“Text”); }
还有其他策略可以做到这一点,比如使用模板。请在此处阅读更多信息:

您让我在“索引”中思考,因此我也能够在没有IList的情况下解决我的问题,谢谢!
@model IList<MVCCodeProject.Models.project>
@for (int i = 0; i < Model.Count(); i++)
{
    Html.DropDownListFor(proj => proj[i], new SelectList(proj.employeeList, "Value", "Text"));
}