javascript - How do I hide an element in the shared layout based on the controller in MVC - Stack Overflow

I have a bootstrap navbar at the top of my page main page and inside it is a text input. I want it hidd

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/home/index
Visible: www.example/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./home/index
Visible: www.example./results
Share Improve this question edited Feb 1, 2015 at 15:14 KyleMit 30.8k72 gold badges511 silver badges702 bronze badges asked Feb 1, 2015 at 5:18 chuckdchuckd 14.7k34 gold badges179 silver badges396 bronze badges 5
  • One possible option would be to assign a value to ViewBag and include/exclude the html based on its value - @if(ViewBag.CanDisplay) {.. – user3559349 Commented Feb 1, 2015 at 5:22
  • Since your navbar is on master/layout page...why not to check the condition there and display the input accordingly. – Vijay Commented Feb 1, 2015 at 5:34
  • How would I check the condition on the master page/layout? – chuckd Commented Feb 1, 2015 at 5:40
  • So this has nothing to do with bootstrap perse, the question is two part. 1. How to pass info from a controller to the shared layout, and 2 How to toggle the visibility of an element based on a property. What have you tried so far regarding either of those? – KyleMit Commented Feb 1, 2015 at 13:53
  • I'm using @if(ViewBag.CanDisplay) to hide data currently – chuckd Commented Feb 2, 2015 at 5:48
Add a ment  | 

1 Answer 1

Reset to default 7

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744275496a4566310.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信