Garry Pilkington


Application Developer
Liverpool, UK

Multiple Strongly Typed Partial Views in MVC

Creating strongly typed views with the ASP.Net MVC framework is really easy, but what if you want to have multiple types on the same view? One way to achieve this is to create partial views for each type and creating a combined view model. Then the view will inherit this combined view model and each partial view will inherit from its component types.

In the following simple example I have a view which itself displays two partial views. This view inherits from a type I have called CombinedViewModel.

The hosting view

   <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
   Inherits="System.Web.Mvc.ViewPage<CombinedViewModel>" %>
   ...
    
   
   <asp:Content ID="Content1" ContentPlaceHolderID="MainContent"
   runat="server">
        
   <% Html.RenderPartial("PartialView1", ViewData.Model.Header); %>
   <% Html.RenderPartial("PartialView2", ViewData.Model.Detail); %>
    
  </asp:Content>

 

The controller passes the types in to the CombinedViewModel.

public ActionResult Detail(int id)
 {
  Header header = DataRepository.GetHeader(id);
  List<Detail> detail = DataRepository.GetDetail(id);
 
  CombinedViewModel viewData = new CombinedViewModel(header, detail);
 
  return View(viewData);         
 }

The CombinedViewModel

public class CombinedViewModel
{
 public Header  Header { get; private set; }
 public IEnumerable<Detail> Detail { get; private set; }  
 
public JobDetailViewModel(Header header, IEnumerable<Detail> detail)
{
 Header = header;
 Detail= detail;
}
}

 

So now each of the partial views can access the types they need.

The PartialViews

<%@ Control Language="C#"

Inherits="System.Web.Mvc.ViewUserControl<TestApp.Models.Header>" %>

<%= Model.CustomerTitle%>

<%@ Control Language="C#"

Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<TestApp.Models.Detail>>" %>

<%= Model.OrderNumber%>
Posted: Oct 20 2009, 09:29 AM by capgpilk | with 6 comment(s)
Filed under: , , ,

Comments

Twitter Trackbacks for Multiple Strongly Typed Partial Views in MVC - Garry Pilkington [asp.net] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Multiple Strongly Typed Partial Views in MVC - Garry Pilkington         [asp.net]        on Topsy.com

# October 29, 2009 2:01 PM

Neil Kerkin said:

Sorry Garry, I don't like this approach as it will require Header header = DataRepository.GetHeader(id); to be repeated in multiple actions. There are plenty of better ways to pass around partial view data.

# October 29, 2009 7:16 PM

capgpilk said:

@Neil - This is the technique I am using on a current MVC application. As always there are multiple ways to do the same thing and it is only through practice that you find a technique you are happy with. As I learn more about MVC, I will perhaps find a better way like you suggest. Do you have any pointers to your suggestions?

# October 30, 2009 4:54 AM

Neil Kerkin said:

@Garry - it may be a little old but Steve Sanderson's blog post is a great resource blog.codeville.net/.../partial-requests-in-aspnet-mvc

He mentions in the comments (when discussing master pages) that view data can be populated using a common base controller or filter attributes (action filters) or any other way you like.

I'll be interested to see what the official standpoint for this is in Asp.Net MVC 2 as it has been quite a contentious topic thus far.

# October 30, 2009 7:00 AM

capgpilk said:

@Neil - Thanks for that, looks a good resource.

# October 30, 2009 8:38 AM

novajoe said:

Awesome! Just what I was looking for. It's what I expected was the answer, but it's great to see an implementation.

# November 8, 2009 3:33 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)