ASP.NET MVC - different models for master page and view page
Asked Answered
M

1

15

I have a strongly typed master page, but I want to use a different type for some of it's child pages.

For example, on the master page...

<%@ Master ... Inherits="System.Web.Mvc.ViewMasterPage<MyWeb.Models.Client>" %>

Client is already a composite object, so on some of the child pages I can keep to the same model and just reference member objects. But on other pages, it won't make sense to do so as I am dealing with a different model, for example, a child page that deals with a Customer model.

My master page still needs the Client model, but the child views will work with different models entirely. The problem is, in a controller, there you can only pass one object model to the View. Is there any way to pass one model to the master and a different one to the view? Thanks!

Mellman answered 23/4, 2009 at 14:43 Comment(2)
Nice Question, I was going to ask the same thing! Top answer though I have to say. As Mastermind says, so simple! Well it is now lol ;-)Upturned
Same dilemma - well stated. Thanks op.Suribachi
N
14

You can create an hierarchy and pass the base model to the master page and child models to your views.

public class BaseModel
{
}

public class ChildModelOne : BaseModel
{
}

public class ChildModelTwo : BaseModel
{
}

This way, you master view will only see its own data (available in master model class) while you views will have access to the extended information available in child model classes.

Very simple.

Nickels answered 23/4, 2009 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.