Jason N. Gaylord
Jason N. Gaylord

Reputation: 8314

Allow Layout View and Other Views to use the same Model

I'm using ASP.NET MVC with Razor syntax. I'm trying to find the best way to obtain data and pass that around my application for access by both the layout view (shared) and other views. In some cases, additional model(s) may need to be passed to views. I've looked at items like the BaseViewModel, but for that, I'd need to pass a model on every ActionResult that would use it. The data changes for every user and could change during the same session.

What's the best way to accomplish that?

Upvotes: 0

Views: 223

Answers (2)

jgauffin
jgauffin

Reputation: 101140

Your question as a bit vague but I think that you are asking how to get a model to the layout?

I would not create a BaseViewModel for that since it isn't really a is-a relationship.

Instead I would do something like I describe here: http://blog.gauffin.org/2011/09/getting-information-into-the-layout-without-using-viewbag/

Upvotes: 3

Gregoire
Gregoire

Reputation: 24832

Create a BaseViewModel and make MyModel a child of this model (ie: public class MyModel:BaseViewModel). In this BaseViewModel put all the common properties that your layout has to use.

After, you need to put in your shared layout:

@model BaseViewModel

Upvotes: 2

Related Questions