Can I not render a strongly typed partial in a strongly typed view?
I am getting the following error when I try to:
The model item passed into the dictionary is of type
‘System.Data.Linq.Table1[UI.Models.vwProject]', but this dictionary1[UI.Models.ProjectStatus]’.
requires a model item of type
'System.Collections.Generic.IEnumerable
I populate both the views using ViewData.Model
public ActionResult Index()
{
ViewData.Model = project.vw_Projects;
return View();
}
public ActionResult ProjectStatus()
{
ViewData.Model = project.ProjectStatus;
return View();
}
Here is my View:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<UI.Models.vwProject>>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div>
<table>
<tr>
<th>
Project
</th>
<th>
Hours
</th>
</tr>
<tr>
<td>
<%= Html.Encode(item.ProjectCode) %>
</td>
<td>
<%= Html.Encode(item.ProjectHours) %>
</td>
</tr>
<div>
<% Html.RenderPartial("ProjectStatus"); %>
</div>
</asp:Content>
Here is my partial:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<UI.Models.ProjectStatus>>" %>
<table>
<tr>
<th>
Code
</th>
<th>
Status
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.Encode(item.ProjectCode) %>
</td>
<td>
<%= Html.Encode(item.ProjectStatus) %>
</td>
</tr>
<% } %>
</table>
I am a little confused with how to display strongly typed/dynamic partials in strongly type or dynamic views. Can someone help me resolve this issue?
If you render a partial it won’t hit you controller action just renders out the view with using the partent’s view model by default.
If you want call your controller action
ProjectStatusthen what you need is the RenderAction method:A good article about When to use RenderAction vs RenderPartial with ASP.NET MVC