Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if content section is provided in RenderSection in ASP.NET MVC 3

Is it was possible to test for the existence of a supplied content section when using the RenderSection helper in ASP.NET MVC 3?

For example:

@RenderSection("RightCrumbContentArea", required: false)

If the above isn't supplied, I wish to generate some other content.

like image 860
Nick Avatar asked Sep 21 '11 10:09

Nick


People also ask

How do you check if a view is partial or not?

Solution 1 There is no difference in the markup between a partial view and a view; the only difference is in how they are delivered to the browser. A partial view is sent via AJAX and must be consumed by JavaScript on the client side, while a View is a full postback. That's it.

What is the difference between RenderBody and RenderSection?

RenderBody() renders all the content of the child view which is not wrapped in the named section. RenderSection() renders only a part of the child view which is wrapped under the named section.

What is the difference between RenderBody and RenderSection in MVC?

RenderBody is required, as it's what renders each view. RenderSection has an optional parameter that lets you mark the section as not required.

How do I use RenderSection in partial view?

You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .


1 Answers

It is possible as far as I know.

Try the following code :

@if (IsSectionDefined("RightCrumbContentArea")) { 

    @RenderSection("RightCrumbContentArea")

} else { 

    <span>poo</span>
}
like image 186
tugberk Avatar answered Sep 30 '22 11:09

tugberk