asp.net-mvc-4asp.net-mvc-partialviewgrid.mvc

How to add a list<T> to view with a single model


Getting an error while trying to add a grid to my detail page. The error is:

The model item passed into the dictionary is of type 'GridMvc.Html.HtmlGrid1[MyApp.Models.RecipientActivityMetadata]', but this dictionary requires a model item of type 'System.Collections.Generic.List1[MyApp.Models.RecipientActivityMetadata]'.

MVC4 View is a combination of a detail page and a list. I am using a viewmodel that looks like this:

public class FormViewModel()
{
    public RecipientMetadata Recipient { get; set; }
    public StudentActivityMetadata StudentActivity    { get; set; }
    public List<RecipientActivityMetadata> RecipientActivites { get; set; }
}

The view top is:

@model MyApp.Models.ViewModels.FormViewModel

and it renders a partial view which contains the list:

@Html.Partial("_grid", Model.RecipientActivites)

and the partial looks like this:

@using GridMvc.Html
@model List<MyApp.Models.RecipientActivityMetadata>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<div>
    @Html.Grid(Model).Columns(columns =>
    {
        columns.Add(c => c.ActCount).Titled("Activity Num");
        columns.Add(c => c.ActivityType).Titled("Activity Type");
        columns.Add(c => c.FundCode).Titled("FundCode");
        columns.Add(c => c.Hours).Titled("Hours");
    }).WithPaging(10)
</div>

Solution

  • From Comment to Answer

    According to the documentation provided by Grid.Mvc, @Html.Grid uses a partial view _Grid.cshtml. Because your partial view also has same name, the solution is to use a different name for your partial view.