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 12 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

masmith said:

I know this post is a bit old now, but is this still your preferred way of doing this? I have this very issue and I'm having problems finding any examples out there. Thanks for posting this.

# July 26, 2010 3:05 PM

capgpilk said:

@masmith - I have implemented this on a large MVC application and had no problems with it. It is a good method when you only have a small number of partial views, but could get cumbersome with more partials. I don't use any other method at this time as this has served me fine. If I do find other ways, I will write an update post.

# July 27, 2010 4:20 AM

Torna said:

I think this is great way to combine multiple modules to a view.

I will try it out and have the controller call sub controllers to get partial view stuff to bind in to the model.

I will use the areas to split the functionality in diffrent projects.

Maybe i use the unity event aggregator with the model as a param to the event.

I like this way it helps me to implement stuff like i do with regions in prism

like to see more cool stuff like this.

# October 31, 2010 3:32 PM

hooher tod said:

Yes there should realize the reader to RSS my feed to RSS commentary, quite simply

# September 14, 2011 9:14 PM

haider said:

Hi,

I have used 6 partial views in my index page and it become very slow to render all views. How to quick render.

# November 25, 2011 6:28 AM

ReageasencyuI said:

Что для Вас Юмор?    

<a href=http://xn--c1aeb8eua.xn--p1ai/>полно картинки девочек lj 18 лет</a>

# January 31, 2012 12:30 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)