As recommend by v开发者_如何学Pythonarious performance optimization tools my webpages all include the JQuery reference at the end of the page. Lately, I've been introducing JQuery code into my partial views. Since JQuery is not referenced yet, how can I use JQuery throughout my page in partial views?
UPDATE: Here's something I'm trying to do. This is in a partial view.
@model pending.Models.WidgetZone
<fieldset id="fieldset_Available-Widgets_@(Model.Slug)" class="available-widgets">
<legend>@i18n.widgets_availableWidgetsList</legend>
@Html.Action(MVC.Admin.WidgetFramework.Select(Model.Slug))
@{
string ddlId = "#select_Available-Widgets_" + @Model.Slug;
string pageId = ViewContext.RouteData.Values["id"].ToString();
}
<p>
@* <input type="button" value="@i18n.widgets_AddWidget" id="btn_AddWidget_@Model.Slug" onclick="pending.widgetAdmin.addWidget($('@ddlId').val(), '@Model.Slug', '@pageId')" />*@
<input type="button" value="@i18n.widgets_AddWidget" id="btn_AddWidget_@(Model.Slug)" />
</p>
</fieldset>
@section js_placeholder {
<script type="text/javascript">
$('#btn_AddWidget_@(Model.Slug)').click(function () {
pending.widgetAdmin.addWidget($('@ddlId').val(), '@Model.Slug', '@pageId');
});
</script>
}
As you can see I have some JQuery selecting code in there that requires JQuery in the <head>
of the page. I've already tried implementing Darin's suggestion (see @section
) but it doesn't render anything.
I read about an extension/helper library for jQuery that covered exactly your problem, but can not remember which one :-(
Perhaps: http://aboutcode.net/knapsack/
There are some 'Scriptmanager' nuget packages that go in the same direction.
Or another possibility: http://assman.codeplex.com/
Include the jQuery code in the _Layout, just before the closing </body>
tag. Partials shouldn't be including any jQuery or javascript code. Add a section in your layout just after the jQuery inclusion and in all pages override this section to include the necessary scripts.
As an alternative you could write some helpers as shown in this answer. It is not something I would recommend but it could be a workaround.
精彩评论