Razor下拉的问题

时间:2011-04-14 19:08:01

标签: asp.net-mvc-3 view drop-down-menu razor

我在db中有2个表:MixedType(id和name)和Block(id,name,idMixedType)。

我想为Block(创建视图)制作强类型视图。

控制器如下:

    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字段下拉。

我尝试按照以下方式进行(根据此回答Create a Dropdown List for MVC3 using Entity Framework (.edmx Model) & Razor Views && Insert A Database Record to Multiple Tables):

    <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

有什么问题?

1 个答案:

答案 0 :(得分:1)

您将Model.MixedType传递给SelectList构造函数。据推测,Model.MixedType不是IEnumerable。

它是否可能是小写的“m”(model.MixedType)?

如果没有,则需要检查静态MixedType属性并确保它是一个实现IEnumerable的集合(并且它枚举的对象具有“id”和“name”属性,但是我认为是这样的。)