0

I am trying to create a partial view to display a list of items but keep running into this error which

The model item passed into the dictionary is of type 'Revision.Models.GroupEvent', but this dictionary requires a model item of type 'System.Collections.Generic.GroupIEnumerable`1[Revision.Models.Event]'.'

This is my Home Controller

public class HomeController : Controller
{
    public RevisionDb db = new RevisionDb();
    EventViewModels evm = new EventViewModels();

    public ActionResult Index()
    {
        evm.Events = db.GroupEvents.ToList();
        return View(new GroupEvent() { EventPartialModel = evm.Events.ToList()} );
    }
}

This is the View Model I am using

public class EventViewModels
{
    public int NumberofEvents { get; set; }
    public List<GroupEvent> Events { get; set; }
    public int EventCount { get; set; }
}

This is my Partial View

@model IEnumerable<Revision.Models.GroupEvent>
@using Revision.Models
@if (Model != null)
{
    <div class="grid">
        <table cellspacing="0" width="80%">
            <thead>
                <tr>
                    <th>Title</th>
                    <th>Description</th>
                    <th>Date</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td align="left">@item.EventTitle</td>
                        <td align="left">@item.EventDesc</td>
                        <td align="left">@item.EventDate</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
}

And this is my main view

@model IEnumerable<Revision.Models.GroupEvent>
@{
    ViewBag.Title = "Index";
}
<a href="~/Models/GroupEvents.cs">~/Models/GroupEvents.cs</a>
<div>
    @Html.Partial("EventsPartialView", Model)
</div>
Cathal O 'Donnell
  • 515
  • 3
  • 10
  • 33
  • Your view needs `@model EventViewModels` and the loop needs to be `@foreach (var item in Model.EventPartialModel) { ...` but `EventViewModels` does not even contain a property named `EventPartialModel` so your code won't even compile. I assume you mean `return View(evm);` –  Dec 06 '15 at 21:42

1 Answers1

1

Your parameter to return View() is new GroupEvent. It is supposed to be an IEnumerable<GroupEvent>. And then your ViewModel that you say you're using is EventViewModels, which is not referenced.

It looks like it should be

MainClass:

return View(new EventViewModels{Events = evm.Events.ToList()})

MainView

@model EventViewModels

@Html.Partial("EventsPartialView", Model.Events)

Kyle W
  • 3,702
  • 20
  • 32