asp.net mvc 4 - How to render Views or Partial Views of scripts at the bottom in _Layout.cshtml? -


i have problem in rendering few of partial view’s script tags should render @ bottom in _layout.cshtml page.

i got solution,you may not want render on every page. sectioning gives control of that.

in _layout .cshtml define scripts tags this,

<script src="/scripts/libs/jquery-ui-1.10.3.min.js"></script> <script src="~/scripts/jquery.validate.min.js"></script> <script src="~/scripts/jquery.validate.unobtrusive.js"></script> @if (issectiondefined("bottomscripts")) {     @rendersection("bottomscripts", required: false)  } 

in yourview.cshtml define section this

@section bottomscripts{ <script src="~/scripts/jquery.unobtrusive-ajax.min.js"></script> } 

here standard structure of layout , views in mvc.

_layout.cshtml

<!doctype html>  <html lang="en-us"> <head>     <meta charset=" utf-8">     <title>@viewbag.title</title>     <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">      <link rel="stylesheet" href="~/content/css/stye1.css" />     <link rel="stylesheet" href="~/content/css/style2.css" />     @if (issectiondefined("mystyles"))     {         @rendersection("mystyles", required: false);     } </head> <body> <header>     <a href="/index">         <h1>my website<small>your slogan</small></h1>     </a> </header> <div>     @renderbody() </div> <script src="~/scripts/script1.js"></script> <script src="~/scripts/script2.js"></script> @if (issectiondefined("myscripts")) {     @rendersection("myscripts", required: false); } </body> </html> 

view1.cshtml

<div>this view1</div> @section mystyles{ <link rel="stylesheet" href="~/pagespecificstyle.css" /> } @section myscripts{ <script src="~/pagespecificscript.js"></script> } 

view2.cshtml

<div>this view2</div> @section mystyles{ <link rel="stylesheet" href="~/pagespecificstyle2.css" /> } @section myscripts{ <script src="~/pagespecificscript2.js"></script> } 

Comments