1

I found this answer, which has helped me understand how to use polymorphic model binding in order to avoid creating a separate controllers for every derived models I have.

However, I didn't quite figure out how to pass a IEnumerable to my EditorTemplates. Normally, by scaffolding my models I would end up with a IEnumerable in my views which I can cycle with a foreach loop in order to display the all content.

Something like this...

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.username)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.date)
        </td>
        ...

In a regular controller I would use return View(items.ToList()); but how can I achieve the same result with polymorphic model binding?

Edit1: This is how my controller looks like..

  public ActionResult Index()
        {

            List<Item> myList = new List<Item>()

            {
                new Derived1(),
                new Derived2()
            };

            var model = myList;
            return View(model);
        }

From index I call @Html.EditorForModel()

And in my EditorTemplates I have this...

@model Gear.Models.Derived1
<h3>Phones</h3>
@Html.Hidden("ModelType", Model.GetType())

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(x => x.username)
        </th>
        <th>
            @Html.DisplayNameFor(x => x.address)
        </th>
        <th></th>
    </tr>

    <tr>

        <td>
            @Html.DisplayFor(x => x.username)
        </td>
        <td>
            @Html.DisplayFor(x => x.address)
        </td>

    </tr>
</table>

Now like you said, it seems like the EditorForModel is doing its job by displaying both derived model in the list but only DisplayNameFor works ...I can't get the field populated with actual data from the DB with DisplayFor. Is there something I'm doing wrong?

Community
  • 1
  • 1
Orphu.of.io
  • 125
  • 1
  • 16
  • I don't see you do anything wrong here. Check whether you actually fill the model (with data from DB) before you return it to the view. – Ε Г И І И О Mar 11 '14 at 13:42

1 Answers1

0

If you already have the EditorTemplates, just call @Html.EditorForModel(), instead of the foreach loop. Depending on the type of the object, the framework will select the correct editor template for rendering.

Ε Г И І И О
  • 11,199
  • 1
  • 48
  • 63
  • When I call @Html.EditorForModel() from within Index.cshtml it renders all of the EditorTemplates that it can find. If I try to call @Html.EditorForModel("templatename") it complains that: "The model item passed into the dictionary is of type 'Project.Models.BaseModel[]', but this dictionary requires a model item of type 'Project.Models.Derived1'. What I'm trying to do is be able to call whichever EditorTemplate for the derived class I need. Any clue? – Orphu.of.io Mar 08 '14 at 12:11
  • How many items are there in the list pased as the model? If you have all the derived types in that list, probably what EditorForModel() doing is right. – Ε Г И І И О Mar 08 '14 at 14:24
  • I added an edit to my original question with more information. Thanks in advance! – Orphu.of.io Mar 08 '14 at 14:39