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?

2
  • Model.MixedType is a simple class(id, name). Commented May 7, 2011 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. Commented May 7, 2011 at 13:54

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.