将数据获取到共享局部视图
遗憾的是,当谈到 .NET MVC 时,我仍然有点不以为然。我有一个从 MasterPage 调用的导航部分视图,我想在其中获取数据列表。该部分视图位于“Shared”文件夹中,因此没有控制器。如何获取数据以便它可以呈现列表?
谢谢
更具体地说,我想做这样的事情(伪代码):
<ul>
<% foreach (item in ListOfItems) {
Response.Write(formattedListItem);
} %>
</ul>
I'm still sadly a bit n00bish when it comes to .NET MVC. I have a navigation partial view being called from a MasterPage where I would like to get a list of data. This partial view is in the "Shared" folder, and thus does not have a controller. How do I get data to it so that it may render a list?
Thanks
More specifically, I want to do something like this (pseudocode):
<ul>
<% foreach (item in ListOfItems) {
Response.Write(formattedListItem);
} %>
</ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
[编辑] 现在我知道了完整的故事,新的有用链接
http://www.superexpert.com/blog/archive/2008/08/12/asp-net-mvc-tip -31-将数据传递到母版页和用户控件.aspx
[Edit] New helpful link now that I know the full story
http://www.superexpert.com/blog/archive/2008/08/12/asp-net-mvc-tip-31-passing-data-to-master-pages-and-user-controls.aspx
您可以像传递常规页面一样传递视图模型。只是,您可以使用辅助方法,而不是在控制器中调用 View(...) 时传入它。类似于:
您传入的数据(在本例中为 Model.MenuItems)可能来自您使用部分视图的视图的模型(如上例中的情况)。
You pass in a view model just like you do for a regular page. Only, instead of passing it in when calling View(...) in the controller, you use a helper method. Something like:
The data you are passing in (Model.MenuItems in this case) will probably come from the Model of the View in which you are using the partial view (as is the case in the example above).
只需使用 RenderAction 即可。 RenderAction 将调用您选择的控制器上的方法并获取您希望显示的结果。您甚至可以从此方法返回 PartialView。
希望有帮助。
Just use RenderAction. RenderAction will call the method on a controller you choose and get the results you wish to display. You can even return a PartialView from this method.
Hope that helps.