Pass data to layout that are common to all pages

2019-01-01 10:50发布

问题:

I have a website which have a layout page. However this layout page have data which all pages model must provide such page title, page name and the location where we actually are for an HTML helper I did which perform some action. Also each page have their own view models properties.

How can I do this? It seems that its a bad idea to type a layout but how do I pass theses infos?

回答1:

If you are required to pass the same properties to each page, then creating a base viewmodel that is used by all your view models would be wise. Your layout page can then take this base model.

If there is logic required behind this data, then this should be put into a base controller that is used by all your controllers.

There are a lot of things you could do, the important approach being not to repeat the same code in multiple places.

Edit: Update from comments below

Here is a simple example to demonstrate the concept.

Create a base view model that all view models will inherit from.

public abstract class ViewModelBase
{
    public string Name { get; set; }
}

public class HomeViewModel : ViewModelBase
{
}

Your layout page can take this as it\'s model.

@model ViewModelBase
<!DOCTYPE html>
<html>
    <head>
        <meta name=\"viewport\" content=\"width=device-width\" />
        <title>Test</title>
    </head>
    <body>
        <header>
            Hello @Model.Name
        </header>
        <div>
            @this.RenderBody()
        </div>
    </body>
</html>

Finally set the data in the action method.

public class HomeController
{
    public ActionResult Index()
    {
        return this.View(new HomeViewModel { Name = \"Bacon\" });
    }
}


回答2:

I used RenderAction html helper for razor in layout.

@{
   Html.RenderAction(\"Action\", \"Controller\");
 }

I needed it for simple string. So my action returns string and writes it down easy in view. But if you need complex data you can return PartialViewResult and model.

 public PartialViewResult Action()
    {
        var model = someList;
        return PartialView(\"~/Views/Shared/_maPartialView.cshtml\", model);
    }

You just need to put your model begining of the partial view \'_maPartialView.cshtml\' that you created

@model List<WhatEverYourObjeIs>

Then you can use data in the model in that partial view with html.



回答3:

Another option is to create a separate LayoutModel class with all the properties you will need in the layout, and then stuff an instance of this class into ViewBag. I use Controller.OnActionExecuting method to populate it. Then, at the start of layout you can pull this object back from ViewBag and continue to access this strongly typed object.



回答4:

Presumably, the primary use case for this is to get a base model to the view for all (or the majority of) controller actions.

Given that, I\'ve used a combination of several of these answers, primary piggy backing on Colin Bacon\'s answer.

It is correct that this is still controller logic because we are populating a viewmodel to return to a view. Thus the correct place to put this is in the controller.

We want this to happen on all controllers because we use this for the layout page. I am using it for partial views that are rendered in the layout page.

We also still want the added benefit of a strongly typed ViewModel

Thus, I have created a BaseViewModel and BaseController. All ViewModels Controllers will inherit from BaseViewModel and BaseController respectively.

The code:

BaseController

public class BaseController : Controller
{
    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        base.OnActionExecuted(filterContext);

        var model = filterContext.Controller.ViewData.Model as BaseViewModel;

        model.AwesomeModelProperty = \"Awesome Property Value\";
        model.FooterModel = this.getFooterModel();
    }

    protected FooterModel getFooterModel()
    {
        FooterModel model = new FooterModel();
        model.FooterModelProperty = \"OMG Becky!!! Another Awesome Property!\";
    }
}

Note the use of OnActionExecuted as taken from this SO post

HomeController

public class HomeController : BaseController
{
    public ActionResult Index(string id)
    {
        HomeIndexModel model = new HomeIndexModel();

        // populate HomeIndexModel ...

        return View(model);
    }
}

BaseViewModel

public class BaseViewModel
{
    public string AwesomeModelProperty { get; set; }
    public FooterModel FooterModel { get; set; }
}

HomeViewModel

public class HomeIndexModel : BaseViewModel
{

    public string FirstName { get; set; }

    // other awesome properties
}

FooterModel

public class FooterModel
{
    public string FooterModelProperty { get; set; }
}

Layout.cshtml

@model WebSite.Models.BaseViewModel
<!DOCTYPE html>
<html>
<head>
    < ... meta tags and styles and whatnot ... >
</head>
<body>
    <header>
        @{ Html.RenderPartial(\"_Nav\", Model.FooterModel.FooterModelProperty);}
    </header>

    <main>
        <div class=\"container\">
            @RenderBody()
        </div>

        @{ Html.RenderPartial(\"_AnotherPartial\", Model); }
        @{ Html.RenderPartial(\"_Contact\"); }
    </main>

    <footer>
        @{ Html.RenderPartial(\"_Footer\", Model.FooterModel); }
    </footer>

    < ... render scripts ... >

    @RenderSection(\"scripts\", required: false)
</body>
</html>

_Nav.cshtml

@model string
<nav>
    <ul>
        <li>
            <a href=\"@Model\" target=\"_blank\">Mind Blown!</a>
        </li>
    </ul>
</nav>

Hopefully this helps.



回答5:

You don\'t have to mess with actions or change the model, just use a base controller and cast the existing controller from the layout viewcontext.

Create a base controller with the desired common data (title/page/location etc) and action initialization...

public abstract class _BaseController:Controller {
    public Int32 MyCommonValue { get; private set; }

    protected override void OnActionExecuting(ActionExecutingContext filterContext) {

        MyCommonValue = 12345;

        base.OnActionExecuting(filterContext);
    }
}

Make sure every controller uses the base controller...

public class UserController:_BaseController {...

Cast the existing base controller from the view context in your _Layout.cshml page...

@{
    var myController = (_BaseController)ViewContext.Controller;
}

Now you can refer to values in your base controller from your layout page.

@myController.MyCommonValue


回答6:

if you want to pass an entire model go like so in the layout:

@model ViewAsModelBase
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">
    <meta charset=\"utf-8\"/>
    <link href=\"/img/phytech_icon.ico\" rel=\"shortcut icon\" type=\"image/x-icon\" />
    <title>@ViewBag.Title</title>
    @RenderSection(\"styles\", required: false)    
    <script type=\"text/javascript\" src=\"http://code.jquery.com/jquery-1.8.3.min.js\"></script>
    @RenderSection(\"scripts\", required: false)
    @RenderSection(\"head\", required: false)
</head>
<body>
    @Html.Action(\"_Header\",\"Controller\", new {model = Model})
    <section id=\"content\">
        @RenderBody()
    </section>      
    @RenderSection(\"footer\", required: false)
</body>
</html>

and add this in the controller:

public ActionResult _Header(ViewAsModelBase model)


回答7:

Creating a base view which represents the Layout view model is a terrible approach. Imagine that you want to have a model which represents the navigation defined in the layout. Would you do CustomersViewModel : LayoutNavigationViewModel? Why? Why should you pass the navigation model data through every single view model that you have in the solution?

The Layout view model should be dedicated, on its own and should not force the rest of the view models to depend on it.

Instead, you can do this, in your _Layout.cshtml file:

@{ var model = DependencyResolver.Current.GetService<MyNamespace.LayoutViewModel>(); }

Most importantly, we don\'t need to new LayoutViewModel() and we will get all the dependencies that LayoutViewModel has, resolved for us.

e.g.

public class LayoutViewModel
{
    private readonly DataContext dataContext;
    private readonly ApplicationUserManager userManager;

    public LayoutViewModel(DataContext dataContext, ApplicationUserManager userManager)
    {
    }
}


回答8:

Other answers have covered pretty much everything about how we can pass model to our layout page. But I have found a way using which you can pass variables to your layout page dynamically without using any model or partial view in your layout. Let us say you have this model -

public class SubLocationsViewModel
{
    public string city { get; set; }
    public string state { get; set; }
}

And you want to get city and state dynamically. For e.g

in your index.cshtml you can put these two variables in ViewBag

@model  MyProject.Models.ViewModel.SubLocationsViewModel
@{
    ViewBag.City = Model.city;
    ViewBag.State = Model.state;
}

And then in your layout.cshtml you can access those viewbag variables

<div class=\"text-wrap\">
    <div class=\"heading\">@ViewBag.City @ViewBag.State</div>
</div>


回答9:

I do not think any of these answers are flexible enough for a large enterprise level application. I\'m not a fan of overusing the ViewBag, but in this case, for flexibility, I\'d make an exception. Here\'s what I\'d do...

You should have a base controller on all of your controllers. Add your Layout data OnActionExecuting in your base controller (or OnActionExecuted if you want to defer that)...

public class BaseController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext     
        filterContext)
    {
        ViewBag.LayoutViewModel = MyLayoutViewModel;
    }
}

public class HomeController : BaseController
{
    public ActionResult Index()
    {
        return View(homeModel);
    }
}

Then in your _Layout.cshtml pull your ViewModel from the ViewBag...

@{
  LayoutViewModel model = (LayoutViewModel)ViewBag.LayoutViewModel;
}

<h1>@model.Title</h1>

Or...

<h1>@ViewBag.LayoutViewModel.Title</h1>

Doing this doesn\'t interfere with the coding for your page\'s controllers or view models.



回答10:

You can also make use of RenderSection , it helps to you to inject your Model data into the _Layout view.

You can inject View Model Data, Json, Script , CSS, HTML etc

In this example I am injecting Json from my Index View to Layout View.

Index.chtml

@section commonLayoutData{

    <script>

        var products = @Html.Raw(Json.Encode(Model.ToList()));

    </script>

    }

_Layout.cshtml

@RenderSection(\"commonLayoutData\", false)

This eliminates the need of creating a separate Base View Model.

Hope helps someone.



回答11:

instead of going through this you can always use another approach which is also fast

create a new partial view in the Shared Directory and call your partial view in your layout as

@Html.Partial(\"MyPartialView\")

in your partial view you can call your db and perform what ever you want to do

@{
    IEnumerable<HOXAT.Models.CourseCategory> categories = new HOXAT.Models.HOXATEntities().CourseCategories;
}

<div>
//do what ever here
</div>

assuming you have added your Entity Framework Database



回答12:

It\'s incredible that nobody has said this over here. Passing a viewmodel through a base controller is a mess. We are using user claims to pass info to the layout page (for showing user data on the navbar for example). There is one more advantage. The data is stored via cookies, so there is no need to retrieve the data in each request via partials. Just do some googling \"asp net identity claims\".



回答13:

You can use like this:

 @{ 
    ApplicationDbContext db = new ApplicationDbContext();
    IEnumerable<YourModel> bd_recent = db.YourModel.Where(m => m.Pin == true).OrderByDescending(m=>m.ID).Select(m => m);
}
<div class=\"col-md-12\">
    <div class=\"panel panel-default\">
        <div class=\"panel-body\">
            <div class=\"baner1\">
                <h3 class=\"bb-hred\">Recent Posts</h3>
                @foreach(var item in bd_recent)
                {
                    <a href=\"/BaiDangs/BaiDangChiTiet/@item.ID\">@item.Name</a>
                }
            </div>
        </div>
    </div>
</div>