0

I have a menu bar on layout page and put it in a section on layout.

@section MenuSection{
<ul class="menubar">
<li>Test</li>
<li>Test</li>
<li>Test</li>
</ul>
}

I have multiple view in application associated with layout page. On some views It won't be requiring this menu bar. So I tried hiding section for that view

Index View-

@{
@RenderSection("MenuSection",required:false)
}

But this menu section disappears from layout.

What is the proper way of doing this? Is section is precise way? What would be a definition of section in layout page?

1
  • 1
    pass information to your layout page. controller/action etc. then put this render in an if statement Commented Nov 26, 2013 at 18:20

2 Answers 2

1
@if (SomeCondition){
    @RenderSection("MenuSection", required: false)
}

The required just means that child pages don't have to specify that @section. But if you want it to display during specific circumstances, place it in an if statement.

Sign up to request clarification or add additional context in comments.

Comments

1

I just looked and we get the controller and action like this

var action = (ViewContext.RouteData.Values["action"] ?? "").ToString().ToLower();
var controller = (ViewContext.RouteData.Values["controller"] ?? "").ToString().ToLower();

the put the render in an if based on this like Brad's answer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.