Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert additional content to page header from viewpage?

Tags:

asp.net-mvc

I am new to asp.net mvc platform. I'm developing with razor template engine in mvc 3. I've created a layout page for all view pages but in some cases I need different page headers for different view pages. For example I have to insert additional script elements to page header to validate data in form pages.

I want to know is there any way to add html element to layout page's header from view page?

Thanks in advance.

like image 655
madyalman Avatar asked Dec 30 '10 08:12

madyalman


People also ask

How do I view Cshtml in my browser?

Right click the Index. cshtml file and select View in Browser. You can also right click the Index. cshtml file and select View in Page Inspector.


1 Answers

You could define a section in the head part of the master page:

<head>
    <script type="text/javascript" src="jquery.js"></script>
    @RenderSection("scripts")
</head>

and in the view define this section:

@section scripts {
    <script type="text/javascript" src="someplugin.js"></script>
}

You also have the possibility to test if a section is defined:

@if (IsSectionDefined("mysection")) { 
    @RenderSection("mysection")
} 
else {
    <div>some default content</div>
}
like image 169
Darin Dimitrov Avatar answered Nov 15 '22 12:11

Darin Dimitrov