MVC3 _Layout.cshtml...禁用/启用菜单项
我有一个 MVC3 C#.Net 项目。当我最初加载我的网站时,我想路由到索引页面并禁用屏幕顶部的导航选项卡,直到做出选择。一旦做出选择,站点就会转到详细信息页面。然后我想启用导航菜单项。我该怎么做?传递 ViewBag 吗?没有把握
I have an MVC3 C#.Net project. When I initially load my Web site, I want to route to an Index page and have the navigation tabs at the top of the screen be disabled until a selection is made. Once a selection is made, the site routes to a details page. I would then like to enable the navigation menu itmes. How can I do this? Pass a ViewBag around? Not sure
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题在于索引页面不遵循网站其余部分的布局,因此索引页面不应使用主布局。
在索引页上:
如果您想在某些条件下呈现菜单,则将菜单存储在部分视图模型中,例如 SiteNavigation.cshtml
然后您可以根据某些条件呈现此视图。例如
I think the problem is that Index page doesn't follow a layout of the rest of the website, therefore index page shouldn't use a master layout.
On your index page:
If you want to render a menu on some condition, then store menu in a partial view model, e.g. SiteNavigation.cshtml
You can then render this view based on some condition. E.g.
我找到了我的问题的答案。 @CodeRush 的答案很酷,我将在另一个场景中使用它。但我喜欢下面针对我的特殊情况的实现。在 Object1Controller Index get Action 中,我添加了以下代码:
然后在 Details get Action 中,即在 Index 视图上单击链接时调用的操作,我添加了以下代码:
然后在 _Layout.cshtml 中添加了以下代码:
呈现为:
或
在其他控制器中,我不需要设置 ViewBag.Hidden 属性,因为可见性属性的默认值是“可见”。对于其他控制器,ViewSource 显示:
哪个呈现为可见
I found an answer to my question. @CodeRush's answer is cool, which I will use in another scenario. But I like the implementation, below, for my prticular situation. In the Object1Controller Index get Action, I added the below code:
Then in the Details get Action, which is the Action called when a link is clicked on the Index view, I added this code:
Then in the _Layout.cshtml I added this code:
Which renders as:
or
In the other Controllers, I don't need to set the ViewBag.Hidden property since the default for the visibility attribute is "visible". For the other Controllers, the ViewSource shows:
Which renders as visible