0

I'm working on a ASP.NET MVC 3 application, but I'm rather new to MVC in general.

I have a partial view in a my application layout view that needs to have data passed to it. this will appear on every page. Is there a way to make this happen so I don't have to load that data into the view model for every action in the entire site?

As in, if a user navigates to Mysite/admin/settings, I would like to have the partial view on the layout be able to somehow receive the data that it needs without me needing to put that code in the Settings action in the Admin controller.

On this same note, how do you pass data to the layout view of an application anyway?

jdavis
  • 8,255
  • 14
  • 54
  • 62
  • possible duplicate of [How to pass data to a PartialView in my layout?](http://stackoverflow.com/questions/7607950/how-to-pass-data-to-a-partialview-in-my-layout) – jrummell Dec 15 '11 at 19:51

3 Answers3

1

Partial only renders a view. You need to provide the model manually.

You can create an action for the view you want and render it with Html.Action( actionName ).


Make an action for example menu which will create a model that will be provided to the menu view.

Now you can call the @Html.Action("menu") from wherever, and it will be rendered autonomously. (you can ofcourse provide a controller name as well, and even custom routeData)

You might also want to set Layout = null; in the view to avoid using the master layout of the whole site.

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • Can you please expand on this. It seems like possible a good solution, but I don't have enough to go on from your post. Could you share some examples or maybe a link to examples? Thanks. – jdavis Dec 15 '11 at 19:52
1

In these situations I usually use a base ViewModel for my Views

public class ApplicationViewModel
{
  public string UserName {get; set;}
  ....
}

public class SettingsViewModel : ApplicationViewModel
{
}

all your views would inherit from that ViewModel. Your layout would expect it as well

_layout.cshtml:

@model ApplicationViewModel
....

<h1>hello @Model.UserName</h1>

hopefully this answers your question

Bassam Mehanni
  • 14,796
  • 2
  • 33
  • 41
  • Sorry - this post http://stackoverflow.com/questions/4154407/asp-net-mvc-razor-pass-model-to-layout screams that assigning a model to a base view is a bad idea. As does a more experienced colleague. Downvoting – Crab Bucket Apr 02 '14 at 08:11
  • 2
    @CrabBucket It does add a restriction, you have to inherit the `ApplicationViewModel` for all views that use the same layout. If that is a restriction you can't live with, then this is not a solution you can use, simple as that. We make tradeoffs all the time. As an experienced programmer, you need to know when to evaluate each situation and decide what works and what doesn't. You live and you learn :) – Bassam Mehanni Apr 02 '14 at 23:51
0

This is how I pass a value to the partial view from my layout page:

Layout page code:

Html.RenderPartial("_SubMenuLeft", new ViewDataDictionary { {"category", "MMG"} });  

and in my _SubMenuLeft.cshtml (partial view)

@if (ViewData["category"] == "MMG")
{
  ...
}

Hope it helps someone for future reference.

Arman H
  • 5,488
  • 10
  • 51
  • 76
Sunil Johnson
  • 1,059
  • 8
  • 6