剃刀的下拉问题

问题描述

| 我在db中有2个表:MixedType(id和名称)和Block(id,名称,idMixedType)。 我想为块创建强类型的视图(创建视图)。 控制器如下:
    public ActionResult Create()
    {
        return View();
    } 
Block()是局部类(我使用Entity Framework + POCO)。 我的文字栏位没有问题,运作正常:
    <div class=\"editor-label\">
        @Html.LabelFor(model => model.name)
    </div>
    <div class=\"editor-field\">
        @Html.EditorFor(model => model.name)
        @Html.ValidationMessageFor(model => model.name)
    </div>
但我想使用来自MixedType表的值对idMixedType字段进行下拉。 我试图通过以下方式做到这一点(根据此答案,使用实体框架(.edmx模型)和Razor视图为MVC3创建一个下拉列表,并向多个表插入数据库记录):
    <div class=\"editor-label\">
        @Html.LabelFor(model => model.idMixedType)
    </div>
    <div class=\"editor-field\">
        @Html.DropDownListFor(model => model.idMixedType,new SelectList(Model.MixedType,\"id\",\"name\"))
        @Html.ValidationMessageFor(model => model.idMixedType)
    </div>
但是我有一个错误
The best overloaded method match for \'System.Web.Mvc.SelectList.SelectList(System.Collections.IEnumerable,string,string)\' has some invalid arguments
怎么了?     

解决方法

您正在将
Model.MixedType
传递给
SelectList
构造函数。大概
Model.MixedType
是IEnumerable。 是否可能应该是小写的\“ m \”(m​​odel.MixedType)? 如果不是,则需要检查静态的
MixedType
属性,并确保它是一个实现
IEnumerable
的集合(并且其枚举的对象具有\“ id \”和\“ name \”属性,但是我认为这是案件)。