简体   繁体   English

如何将数据放入母版页?

[英]How do I put data in a Masterpage?

How do you put a strongly typed object in ASP.NET MVC into the master page? 如何将ASP.NET MVC中的强类型对象放入母版页?

Do you have a ViewModelBase class that contains master page information and inherit from it for every view model, or is there a better approach? 您是否有一个包含母版页信息的ViewModelBase类,并为每个视图模型继承它,还是有更好的方法?

Alex, 亚历克斯,

I think what you are asking is, "Where is my master page controller?" 我想你问的是,“我的主页控制器在哪里?”

Have a look at the following link. 看看以下链接。 It explains how to create an "Application Controller," an abstract class that can be inherited by your other controllers, so that you only have to write the code once that pushes your needed master page data into the view. 它解释了如何创建“应用程序控制器”,这是一个可以由其他控制器继承的抽象类,这样您只需编写一次代码就可以将所需的母版页数据推送到视图中。

Passing Data to View Master Pages: 将数据传递到查看母版页:
http://www.asp.net/learn/MVC/tutorial-13-cs.aspx http://www.asp.net/learn/MVC/tutorial-13-cs.aspx

Also, have a look at the following link, which explains how to implement Partial Views and Subcontrollers in ASP.NET MVC: 另外,请查看以下链接,该链接解释了如何在ASP.NET MVC中实现部分视图和子控制器:

Partial Requests in ASP.NET MVC ASP.NET MVC中的部分请求
http://blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc/ http://blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc/

That is exactly the approach that I use. 这正是我使用的方法。 Have a MasterViewData base class containing information that might be common to all pages and is used to render the master page (logged in user when not using built-in auth, page-level messages). 拥有一个MasterViewData基类,其中包含可能对所有页面通用的信息,并用于呈现母版页(在不使用内置身份验证,页面级消息时登录用户)。 All my other view data classes derive from it. 我的所有其他视图数据类都源自它。

I also do what Robert mentions: I have a base controller class that overrides the View method, which actually handles putting some of the master page information into the viewdata classes. 我也做罗伯特提到的:我有一个基本控制器类来覆盖View方法,它实际上处理将一些母版页信息放入viewdata类。

I'm curious if there are other options, but this approach has definitely worked well for me. 我很好奇是否有其他选择,但这种方法对我来说肯定很有效。

We use a similar base ViewData, particularly for large content-oriented public sites where you've got alot of common UI stuff going on. 我们使用类似的基本ViewData,特别是对于大型面向内容的公共站点,您可以在其中获得大量常见的UI内容。

The trick we use to inject the common bit is to use an ActionFilter to inject the MasterPageViewModel around the our controllers return. 我们用来注入公共位的技巧是使用ActionFilter在我们的控制器返回周围注入MasterPageViewModel。 This is quite a bit cleaner than having a special controller class with an overridden view method as there are definitely places where you don't want/need it. 这比使用带有重写视图方法的特殊控制器类要清晰得多,因为肯定有些地方你不需要/不需要它。 And the whole composition over inheritance thing. 而整个组成超过了继承的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM