问题描述:

In my ASP.NET MVC site all pages share the same content (header & top menu).

What are my options to generate that content is only one place and reuse it in all pages. I also want the link the user is currently on to be handled appropriately (not show up as a link).

网友答案:

In my ASP.NET MVC 2 application, I use a Master Page as suggested by Rich. I just wanted to add that I use a Master Model that all of my view models inherit from to return dynamic data that is needed in the master page (i.e. something to help you determine the active link to highlight). Every time I render a view I explicitly pass it a view model so those values from the master model are always available.

That master model gets its data from things that are globally available (i.e. my custom identity, http context properties, etc.) - because I don't want to manually initialize those properties every time I render a view (which defeats my goal of thin controllers).

网友答案:

In MVC3 this is usually done in the layout page: http://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx

网友答案:

You can also use ASP.NET Master pages. http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx If you start a new ASP MVC project in Visual Studio I believe it'll give you a masterpage in /Views/Shared

网友答案:

If it's dynamic data; then you can always use a RenderAction() inside of a Masterpage. Is this dynamically generated data?

相关阅读:
Top