Mark Bonafe Mark Bonafe -4 years ago 63
C# Question

MVC 5 Cannot get RenderPage or RenderPartial to Work Properly

There are lots of questions and answers concerning this topic, but I am not having any luck getting this to work.

I have two views. One represents a parent, the other is child records. Everything is read only so the views are very simple.

Here is my parent view:

@model IEnumerable<WebApplication1.vw_CurrentDump>

@{
ViewBag.Title = "CurrentDump";
Layout = "~/Views/Shared/_MyLayout.cshtml";
}
@section head{
<link href="@Url.Content("~/Content/css/style.css")" rel="stylesheet" type="text/css" } />
}
<style>
.headerTest {
text-align: center;
width: 100%;
}
</style>

<h2 class="headerTest">Current Dump</h2>

<table class="aTable">
<tr>
<th>
Dump #
</th>
<th>
Units
</th>
<th>
Start
</th>
<th>
Cars
</th>
<th>
Weight
</th>
<th>
Cars Dumped
</th>
<th>
Tons Dumped
</th>
<th>
Destination
</th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.DumpUnitId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Units)
</td>
<td>
@Html.DisplayFor(modelItem => item.StartTime)
</td>
<td>
@Html.DisplayFor(modelItem => item.Cars)
</td>
<td>
@Html.DisplayFor(modelItem => item.Weight)
</td>
<td>
@Html.DisplayFor(modelItem => item.CarsDumped)
</td>
<td>
@Html.DisplayFor(modelItem => item.TonsDumped)
</td>
<td>
@Html.DisplayFor(modelItem => item.Desintation)
</td>
</tr>
}

</table>
@{ Html.RenderPartial("CurrentDumpCars"); }


Here is the view for the child records:

@model IEnumerable<WebApplication1.vw_CurrentDumpCars>

@{
Layout = null;
}
<table class="aTable">
<tr>
<th>
Car Number
</th>
<th>
Date Dumped
</th>
<th>
Weight
</th>
<th>
Empty Track
</th>
</tr>

@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CarNumber)
</td>
<td>
@if (item.DateDumped.HasValue)
{
@item.DateDumped.Value.ToString("MM/dd/yyyy HH:mm")
}
</td>
<td>
@Html.DisplayFor(modelItem => item.Weight)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmptyTrackNumber)
</td>
</tr>
}

</table>


I have tried @RenderPage and RenderPartial (as shown). Both return the same error:


System.InvalidOperationException: 'The model item passed into the dictionary is of type 'System.Collections.Generic.List
1[WebApplication1.vw_CurrentDump]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[WebApplication1.vw_CurrentDumpCars]'.'


It seems to be telling me that I'm passing in vw_CurrentDump to the RenderPartial method, but that doesn't make any sense. Both views are in an Entity Framework 6 library. Everything I read tells me this should work.

Answer Source

CurrentDumpCars takes @model IEnumerable<WebApplication1.vw_CurrentDumpCars> model but your @{ Html.RenderPartial("CurrentDumpCars"); } you did not pass any value or correct value. if CurrentDumpCars has a separate action then try @{ Html.RenderAction("CurrentDumpCars"); } rather than @{ Html.RenderPartial("CurrentDumpCars"); } or pass IEnumerable<WebApplication1.vw_CurrentDumpCars> model into@{ Html.RenderPartial("CurrentDumpCars"); }

Example :

@{
var items= new IEnumerable<WebApplication1.vw_CurrentDumpCars>()//your data
}

@{  Html.RenderPartial("CurrentDumpCars",items); }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download