Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I hide an element in the shared layout based on the controller in MVC

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
like image 262
user1186050 Avatar asked Mar 18 '23 04:03

user1186050


1 Answers

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
like image 194
KyleMit Avatar answered Mar 19 '23 21:03

KyleMit