ASP.NET MVC “Components”

独自空忆成欢 提交于 2019-12-18 15:33:17

问题


Is there someway to have a part of the page that renders like a little sub-page, like components?

For example, if I have a shopping cart on all my pages?


回答1:


If you want it to render another controllers action, as a component, to get encapsulation, you use.

Html.RenderAction

uses routedata to get you there, has its own viewdata and kind of mini life cycle




回答2:


Using preview 5, Html.RenderPartial is your man, you can render sub-controls, and pass them your viewdata, or an arbitrary model, and new viewdata combo.




回答3:


You can create an ActionFilter that modifies the view data. That way, you can decorate every action that returns the partial with the action filter. Take a look at my post:

http://stephenwalther.com/blog/archive/2008/08/12/asp-net-mvc-tip-31-passing-data-to-master-pages-and-user-controls.aspx




回答4:


You are looking for subcontrollers. This implementation is the best way to do what you are talking about.

Edit: I just posted about this here: http://mhinze.com/subcontrollers-in-aspnet-mvc/



来源:https://stackoverflow.com/questions/73902/asp-net-mvc-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!