chuckd
chuckd

Reputation: 14600

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

Upvotes: 4

Views: 5803

Answers (1)

KyleMit
KyleMit

Reputation: 30107

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:

Upvotes: 7

Related Questions