在 MVC 应用程序中将母版页代码放在哪里?
我在 ASP.NET MVC 应用程序中使用了几个(2 或 3)个母版页,它们必须各自显示数据库中的一些信息。 例如赞助商列表、当前资金状况等。
所以我的问题是,我应该将这些母版页数据库调用代码放在哪里?
通常,这些应该进入它自己的控制器类,对吧? 但这意味着我必须手动连接它们(例如传递 ViewDatas),因为它超出了 MVC 框架提供的正常路由框架。
有没有一种方法可以干净地实现这一点,而无需手动将 ViewData 传递/操作调用连接到母版页或对框架进行子类化?
文档数量非常少...而且我对所有这些都非常陌生,包括 MVC 本身的概念,因此请分享您的技巧/技术。
I'm using a few (2 or 3) master pages in my ASP.NET MVC application and they must each display bits of information from the database. Such as a list of sponsors, current fundings status etc.
So my question was, where should I put these master-page database calling code?
Normally, these should goes into its own controller class right? But then that'd mean I'd have to wire them up manually (e.g. passing ViewDatas) since it is out of the normal routing framework provided by the MVC framework.
Is there a way to this cleanly without wiring ViewData passing/Action calls to master pages manually or subclassing the frameworks'?
The amount of documentation is very low... and I'm very new to all this including the concepts of MVC itself so please share your tips/techniques on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不介意强类型视图数据,则可以将所有母版页数据放在 viewData 的公共基类中。 您可以在基类的构造函数中设置此数据。 所有需要附加数据的视图都需要从该基类继承的强类型视图数据。
要允许在没有任何显式视图数据的情况下调用控制器中的 View(),您可以覆盖 ControllerBase 中的 View:
If you don't mind strongly typed view data, you can put all the master page data in a common base class for viewData. You can set this data in the base class's constructor. All your views requiring additional data will then need strongly typed viewdata that inherits from this base class.
To allow a call to View() in your controllers without any explicit viewdata you can override View in your ControllerBase:
一种方法是在母版页视图中放入 ViewData 的挂钩,然后定义一个 BaseController : Controller (或多个基类),在其中执行所需的所有数据库调用。
您想要做的事情与 此文章。
我希望这有帮助!
问候
One way to do this is to put in the masterpage view the hook for the ViewData and then you define a BaseController : Controller (or multiple base classes) where you do all the db calls you need.
What you wanna do is quite the same thing described in this articles.
I hope this helps!
Regards
很好的问题。 您有多种选择。
我认为最好的选择是为母版页中需要填充数据的区域创建控件。 因此,您的母版页将严格包含设计元素。 祝你好运。
Great question. You have several options available to you.
I think the best choice is creating controls for the region of your masterpage that has data that needs to be populated. Thus leaving your masterpage to strictly contain design elements. Good luck.