in my Layout file (I use the Razor syntax) I have included the jQuery script library.
In my partialView, I have jPaginate script library and the code that generates this plugin. The problem is, in that scenario that plugin is not being rendered, but if I move the jQuery script library to the PartialView, it works. But I can’t move the jQuery script library from the Layout.
So, how to access the scripts included in the Layout page in the
PartialView ?
Or maybe there is other solution ?
the code:
Layout.cshtml
<head>
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
</head>
<body>
@RenderBody()
@RenderSection("Scripts", false)
</body>
MyView.cshtml
@{ Html.RenderAction("SomeAction", "MyContoller"); } //it outputs the PartialView.vshtml
@section Scripts {
<script src="@Url.Content("~/Scripts/jquery.paginate.js")" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#pages").paginate({
count: 12,
start: 1,
display: 10,
border: false,
text_color: '#888',
background_color: '#EEE',
text_hover_color: 'black',
background_hover_color: '#CFCFCF'
});
});
</script>
}
PartialView.cshtml
<link href="@Url.Content("~/Content/jpaginate.css")" rel="stylesheet" type="text/css" />
<div id="pages"></div>
Where is your jQuery script rendered in the Layout? Is it towards the end of the layout? If this is the case then when you include the plugin in your partial, jQuery is not yet defined. I would recommend you defining a section in your layout towards the end that will contain custom included scripts:
and then in the view that is using that partial (not in the partial, a partial shouldn’t be dealing with any scripts, it should be markup only) override this section to include any plugins this view requires:
This way all scripts that this page requires will get rendered at the end of the markup which is the recommended approach.