0

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

Community
  • 1
  • 1
Sir Hally
  • 2,318
  • 3
  • 31
  • 48
  • Model.MixedType is a simple class(id, name). – Sir Hally May 07 '11 at 13:53
  • It was my stupid question, I am sorry. I didn't understand the sense of Html.DropDownListFor(model => model.idMixedType, new SelectList(Model.MixedType, "id", "name")) - so, my action returned null for Model. Now, I have solved it. – Sir Hally May 07 '11 at 13:54

1 Answers1

1

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

Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275