将viewdata传递给asp.net mvc母版页 [英] passing viewdata to asp.net mvc masterpages

查看:106
本文介绍了将viewdata传递给asp.net mvc母版页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将ViewData传递给我在asp.net mvc母版页上保留的mvc用户控件.例如,我创建了一个名称下拉列表作为mvc用户控件,并将其放在我的母版页中.

我遇到的问题是将ViewData传递到母版页.我从Microsoft找到这篇文章,这篇文章有一个不错的解决方案,但我想知道是否还有其他更好"的解决方案.我对链接中的解决方案不满意的是,我必须更改每个控制器以从新的控制器类继承.

http://www.asp.net/learn/MVC/tutorial-13-cs.aspx

编辑:我要解决的问题是,如果我在依赖ViewData的母版页中放置用户控件,则必须 REPEATEDLY (重复)添加该ViewData对于使用所述母版页的每个页面.上面链接中的解决方案可能是最好的解决方案,但我希望还有其他选择.

解决方案

对于它的价值,我正在当前项目中使用该教程中的方法,并且效果很好.

如果数据有些静态(例如菜单变化不大),您还可以做的是将对象放在缓存中,这样就不会在每次控制器初始化时都调用数据库. /p>

I'm trying to pass ViewData to my asp.net mvc masterpage for an mvc usercontrol that I keep on a masterpage. For example, I created a dropdownlist of names as an mvc usercontrol and I put that in my masterpage.

The problem I am running into is passing the ViewData to the masterpage. I found this article from Microsoft which has a decent solution but I was wondering if there are other "better" solutions out there. The thing I don't like about the solution in the link is that I have to change every controller to inherit from a new controller class.

http://www.asp.net/learn/MVC/tutorial-13-cs.aspx

Edit: The problem I am looking at is the fact that if I place a user control in my masterpage that relies on ViewData, I have to REPEATEDLY include that ViewData for every single page that uses said masterpage. It's possible the solution in the link above is the best solution but I was hoping there were other alternatives.

解决方案

For what it's worth, I am using the method from that tutorial in a current project and it works very well.

What you can also do, if it is data that is somewhat static (like a menu that doesn't change much), is to put the object on the cache so your database isn't called for every controller initialisation.

这篇关于将viewdata传递给asp.net mvc母版页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆