如何基于MVC中的控制器隐藏共享布局中的元素(How do I hide an element in the shared layout based on the controller in MVC)

编程入门 行业动态 更新时间:2024-10-25 04:21:20
如何基于MVC中的控制器隐藏共享布局中的元素(How do I hide an element in the shared layout based on the controller in MVC)

我的页面主页顶部有一个bootstrap导航栏,里面是文本输入。 我希望它隐藏,直到执行搜索并且用户在results控制器中看到results 。

这是在我的bootstrap导航栏中。

<input type="text" class="form-control" placeholder="Search">

例如:

Hidden: www.example.com/home/index Visible: www.example.com/results

I have a bootstrap navbar at the top of my page main page and inside it is a text input. I want it hidden until a search is performed and the user sees the results in the results controller .

This is inside my bootstrap navbar.

<input type="text" class="form-control" placeholder="Search">

For example:

Hidden: www.example.com/home/index Visible: www.example.com/results

最满意答案

在这种情况下,您实际上不需要将任何特定信息从控制器传递到共享布局。

共享布局可以相应地检查当前路线和行为。

@if (ViewContext.RouteData.Values["Controller"].ToString() == "results") { <input type="text" class="form-control" placeholder="Search"> }

更多信息

将数据传递到ASP.NET MVC中的母版页 在ASP.NET MVC 3中使用HtmlHelper获取当前视图的URL 如何以编程方式在ASP.NET MVC中显示/隐藏Razor View中的区域

In this case, you actually don't need to pass any specific information from the controller to the shared layout.

The shared layout can inspect the current route and the behave accordingly.

@if (ViewContext.RouteData.Values["Controller"].ToString() == "results") { <input type="text" class="form-control" placeholder="Search"> }

More Info:

Passing data to Master Page in ASP.NET MVC Get Current View's Url with HtmlHelper in ASP.NET MVC 3 How to show/hide an area within Razor View in ASP.NET MVC programmatically

更多推荐

本文发布于:2023-07-25 10:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1260040.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   布局   元素   hide   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!