Reputation: 4966
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.Table
1[UI.Models.vwProject]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[UI.Models.ProjectStatus]'.
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?
Upvotes: 0
Views: 193
Reputation: 139758
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 ProjectStatus
then what you need is the RenderAction method:
<% Html.RenderAction("ProjectStatus"); %>
A good article about When to use RenderAction vs RenderPartial with ASP.NET MVC
Upvotes: 1
Reputation: 15583
Yes you can. But if you don't pass a model to RenderPartial it will use the model from view. So, you need to do something like that:
Html.RenderPartial("ProjectStatus", new List<ProjectStatus>()); %>
Upvotes: 2