开发者

Problem with dropdown in Razor

开发者_如何学JAVAI have 2 table in db: MixedType(id and name) and Block(id, name, idMixedType).

I want to make strongly-typed view for Block (Create view).

Controller is following:

    public ActionResult Create()
    {
        return View();
    } 

Block() is a partial class (I use Entity Framework + POCO).

I have no problem with text fields, it works fine:

    <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>

But I want to make dropdown for idMixedType field with values from MixedType table.

I tried to do it in following way (according to this answer 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>

But I have a error

The best overloaded method match for 'System.Web.Mvc.SelectList.SelectList(System.Collections.IEnumerable, string, string)' has some invalid arguments

What is wrong?


You're passing in Model.MixedType to the SelectList constructor. Presumably Model.MixedType is not IEnumerable.

Is it possible it should be a lowercase "m" (model.MixedType)?

If not, you need to review the static MixedType property and make sure it is a collection that implements IEnumerable (and that the objects it enumerates have "id" and "name" properties, but I presume that's the case).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜