使用 ViewData asp.net mvc 帮助
在我的母版页中,我有一个使用 jquery ui 手风琴的菜单。
指定哪个项目应该处于活动状态的最佳方法是什么?我唯一能想到的是
$(document).ready(function() {
$("#accordion").accordion({
collapsible: true,
autoHeight: false,
active:<%=ViewData["active"] %>
});
})
但是每次在我的整个应用程序中调用视图时都必须设置 ViewData["active"] 似乎有点重复......你觉得怎么样?
In my master page I have a menu that uses jquery ui accordion.
What is the best way to specify which item should be active?? the only thing I can think of is
$(document).ready(function() {
$("#accordion").accordion({
collapsible: true,
autoHeight: false,
active:<%=ViewData["active"] %>
});
})
But it seems a little repetitive having to set ViewData["active"] everytime a View is called throughout my whole app... what do you think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否考虑过将手风琴放在 PartialView 中?然后,将代码放置在 PartialView 中并远离母版页。
此外,您还可以拥有一个基本控制器,它可以为您设置 ViewData,因此现在它位于一个位置。
编辑
它与任何其他基类相同。您的控制器继承自
Controller
,因此您的基类也需要执行相同的操作;现在将控制器类设置为从 ControllerBase 类继承;
我还实现了 CacheHelper,但您显然可以使用自己的风格来存储当前值。
Have you considered putting the accordian in a PartialView? Then you place your code within the PartialView and away from the Master page.
Also you can have a base controller which can set the ViewData for you so now it's in one place.
Edit
It's the same as any other base class. Your controller inherits from
Controller
so your base class will need to do the same;Now set your controller class to inherit from the ControllerBase class;
I've also implemented a CacheHelper but you can obviously use your own flavour of storing the current value.