I've got a View rendering two dropdownlists. The controllers for the dropdownlists work fine. They call methods in a repository class for the DB selections. Below the dropdownlists I'm trying to render a table of data in a partial view, in response to the dropdownlist selections.
The dropdowns in the View use a single model:
@model BudgetDemo.Models.BudgetsActualsViewModel
The Partial View displaying the table data uses IEnumerable:
@model IEnumerable<BudgetDemo.Models.BudgetsActualsViewModel>
View (GetBudgetsActuals.cshtml):
@using (Html.BeginForm("GetBudgetsActuals", "BudgetsActuals", FormMethod.Post))
{
... DropDownLists and Submit button
@if (Model.SelectedDepartment != null && Model.SelectedYear != null)
{
// Table headers
@if (Model != null)
{
Html.RenderPartial("_BudgetsActuals", Model);
}
}
}
Partial View (_BudgetsActuals.cshtml):
@model IEnumerable<BudgetDemo.Models.BudgetsActualsViewModel>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Account)
</td>
<td>
@Html.DisplayFor(modelItem => item.CostCenter)
</td>
<td>
@Html.DisplayFor(modelItem => item.TotalCurrentMonthActual)
</td>
<td>
@Html.DisplayFor(modelItem => item.TotalCurrentMonthBudget)
</td>
<td>
@Html.DisplayFor(modelItem => item.TotalYTDActual)
</td>
<td>
@Html.DisplayFor(modelItem => item.TotalYTDBudget)
</td>
<td>
@Html.DisplayFor(modelItem => item.TotalVariance)
</td>
<td>
@Html.DisplayFor(modelItem => item.TotalETCBudget)
</td>
<td>
@Html.DisplayFor(modelItem => item.TotalEAC)
</td>
</tr>
}
Controllers:
// GET: Render view with dropdowns
public ActionResult GetBudgetsActuals()
{
try
{
// Populate Department dropdown and Year dropdown here
repo = new BudgetDemoRepository();
ModelState.Clear();
return View(repo.GetBudgetsActuals());
}
catch
{
return View("Error");
}
}
// POST: Grab data for department and year
[HttpPost]
public ActionResult GetBudgetsActuals(BudgetsActualsViewModel model)
{
repo = new BudgetDemoRepository();
if (ModelState.IsValid)
{
return View(repo.GetBudgetsActuals(model));
}
else
{
model.Departments = repo.GetBudgetsActuals().Departments;
model.Years = repo.GetBudgetsActuals().Years;
return View(model);
}
}
[ChildActionOnly]
public ActionResult
GetBudgetsActualsPartialData(BudgetsActualsViewModel model)
{
repo = new BudgetDemoRepository();
List<BudgetsActualsViewModel> dataVM =
repo.GetBudgetsActualsData(model);
// RETURNING CORRECT DATA
return PartialView("GetBudgetsActuals", dataVM);
}
What I'm trying to figure out is how to hook this all together. It's blowing up here:
@if (Model != null)
{
Html.RenderPartial("_BudgetsActuals", Model);
}
This is the error error message:
The model item passed into the dictionary is of type
'System.Collections.Generic.List`1[BudgetDemo.Models.BudgetsActualsViewModel]',
but this dictionary requires a model item of type
'BudgetDemo.Models.BudgetsActualsViewModel'.
UPDATE
There seems to be a known issue with partial view if you're passing a model to the RenderPartial helper method and that model is null - it will default to the model of the containing view. I have debugged this and from what I can see the model being passed to the helper method is not null, so I am at a loss.
UPDATE 2
Apparently I was not alone regarding the issue of the model of the calling view being passed to the partial view. This problem is addressed in .NET Core with the introduction of View Components :)