1

I want to display 4 different lists from 4 different tables, I created a Partial View for 4 tables in their respected controller, but its not getting rendered, its giving an error;

The model item passed into the dictionary is of type 'Medical_App.Models.MA_Area', but this > dictionary requires a model item of type > 'System.Collections.Generic.IEnumerable`1[Medical_App.Models.MA_Area]'.

If there is another way / alternative of doing this, please share.

  public PartialViewResult AreaList()
    {
        var result = db.MA_Area.OrderBy(d => d.AreaName);
        return PartialView(result);
    }



@model IEnumerable<Medical_App.Models.MA_Area>
        @Html.DisplayNameFor(model => model.CityId)
        @Html.DisplayNameFor(model => model.AreaName)

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.CityId)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.AreaName)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.AreaId }) |
        @Html.ActionLink("Details", "Details", new { id=item.AreaId }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.AreaId })
    </td>
</tr>

}

3
  • Can you show how your controllers and views?
    – SWeko
    Commented Aug 13, 2013 at 10:58
  • Post your views. You are passing a single instance as model while your view is strongly typed to a sequence. Commented Aug 13, 2013 at 10:58
  • any tutorial on this topic Commented Aug 13, 2013 at 11:02

3 Answers 3

1

It sounds like you need to pass the correct model to the partial view. By default calling a partial view passes the model which the parent view has.

So if you are passing the 4 lists to the main view then each partial call should include the correct IEnumarable object for the view.

You can pass a model with a partial call like this:

 @Html.Partial("_partialName", model)

Hope this helps

1

You are passing in a collection but trying to display a single instance. You may need something like:

@model IEnumerable<Medical_App.Models.MA_Area>
@{foreach(var item in Model)
{
@Html.DisplayNameFor(model => item .CityId)
@Html.DisplayNameFor(model => item .AreaName)
}

Then you will need to proved some layout structure for each item.

Good luck

0

You passed list of items i.e.(List) from your controller/view. But you create the pertial views and accept single item of model i.e. Medical_App.Models.MA_Area on the top of your pertial view(@model Medical_App.Models.MA_Area). Pass single instance of model from your controller/view or accept list of model in your pertial view.

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.