MVC布局模型设计模式
我网站中的每个页面都应该有一些相同的数据,类似于每个页面在页面顶部显示有关当前用户的信息。我实现此方法的方法是拥有一个基本控制器类,我的所有控制器都派生自该类。在该基本控制器的构造函数中,我将模型放入 ViewBag 中,然后我的布局页面使用它。
我遇到了问题,因为我的布局不是强类型的。例如,我必须在布局页面中构造新的 HtmlHelpers:
@{var layoutHtml = new HtmlHelper<LayoutModel>(Html.ViewContext, Html.ViewDataContainer); }
@* OK, now we can use the html helper... *@
@layoutHtml.TextAreaFor(model => model.Feedback)
我真的不想让我的模型子类来自布局模型,因为这将迫使每个操作单独填写共享模型数据,而是手动创建HtmlHelpers 似乎也是一个坏主意。
有什么想法吗?
Every page in my site should have some of the same data, similar to how in SO every page displays info about the current user at the top of the page. The way I implemented this was to have a base controller class that all my controllers derive from. In that base controller's constructor I put my model in the ViewBag, and then my layout page uses that.
I'm running into problems with this because my layouts aren't strongly typed. For example, I have to construct new HtmlHelpers in the layout page:
@{var layoutHtml = new HtmlHelper<LayoutModel>(Html.ViewContext, Html.ViewDataContainer); }
@* OK, now we can use the html helper... *@
@layoutHtml.TextAreaFor(model => model.Feedback)
I really don't want to have to have my models subclass from a layout model, since that would force each action to fill out the shared model data individually, but manually creating HtmlHelpers also seems like a bad idea.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
基础控制器是一个很好的开始方式。我还将介绍一个基本视图模型。基本视图模型将包含用户特定信息。
然后,根据每个请求,您将使用用户特定信息以及视图所需的任何信息填充视图模型。 MyViewModel 只是继承自 BaseViewModel 的视图模型。
在主视图中,我将传递 BaseViewModel 和我将传递继承的 MyViewModel 的视图。
现在,您可以在主视图中访问用户信息,并将其传递给部分视图或直接将其呈现到页面。
A base controller is a great way to start. I would also introduce a base viewmodel. The base viewmodel would contain user specific information.
Then on each request you would populate your viewmodel with user specific information and whatever information is required for the view. MyViewModel is just a viewmodel that inherits from BaseViewModel.
In the master view I would pass in the BaseViewModel and the View I would pass in the inherited MyViewModel.
Now you have access to your user information in your master view and can pass it to a partial or render it directly to the page.
您可以定义多个布局,您可以在适当的视图中使用它们!只需像这样包含它们:
you can define multiple Layouts which you can use in your appropiate views! Just include them like so:
我相信你可以使用RenderAction来解决这个问题。由于此操作将显示的信息在所有页面上都是通用的,因此您可以将其放入 BaseController 中并从站点主机调用它。它将计算自己的模型并将该模型返回到可以强类型化的部分视图,并且您不必像现在那样实例化 htmlHelper 。
i believe you can use RenderAction to solve this problem. Because information this action will display is common on all pages, you can put it in BaseController and call it from your site master. it will compute its own model and return that model to partial view which can be strongly typed and you don't have to instantiate htmlHelper the way you are doing now.
我可能是错的,但你不应该为此使用部分视图吗?
您的解决方案看起来太复杂了。
I might be wrong, but shouldn't you use partial views for this?
Your solution looks way too complicated.