Inside my div element I am updating is a page partial with a webgrid and when I specifiy ajaxUpdateContainerId: in the webgrid constructor I get the error:
JavaScript runtime error: 'jQuery' is undefined
Here is the generated javascript the error references:
<script type="text/javascript">
(function($) {
$.fn.swhgLoad = function(url, containerId, callback) {
url = url + (url.indexOf('?') == -1 ? '?' : '&') + '__swhg=' + new Date().getTime();
$('<div/>').load(url + ' ' + containerId, function(data, status, xhr) {
$(containerId).replaceWith($(this).html());
if (typeof(callback) === 'function') {
callback.apply(this, arguments);
}
});
return this;
}
$(function() {
$('table[data-swhgajax="true"],span[data-swhgajax="true"]').each(function() {
var self = $(this);
var containerId = '#' + self.data('swhgcontainer');
var callback = getFunction(self.data('swhgcallback'));
$(containerId).parent().delegate(containerId + ' a[data-swhglnk="true"]', 'click', function() {
$(containerId).swhgLoad($(this).attr('href'), containerId, callback);
return false;
});
})
});
function getFunction(code, argNames) {
argNames = argNames || [];
var fn = window, parts = (code || "").split(".");
while (fn && parts.length) {
fn = fn[parts.shift()];
}
if (typeof (fn) === "function") {
return fn;
}
argNames.push(code);
return Function.constructor.apply(null, argNames);
}
})(jQuery);
Here is my main page:
@model IEnumerable<PamperWeb.Models.Lab>
@{
ViewBag.Title = "Index";
}
@Html.Partial("_SubMenu")
<h2>Index</h2>
<p>
@Html.ActionLink("Add Lab", "Create", "Lab", new { patientId = ViewBag.PatientId }, null)
</p>
<div id="labs">
@Html.Partial("_Completed", Model)
</div> @section Scripts {
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/sitespec")
}
And the partial:
@model IEnumerable<PamperWeb.Models.Lab>
@{
var grid = new WebGrid(Model, ajaxUpdateContainerId: "labs", defaultSort: "Name");
}
@grid.GetHtml(columns: grid.Columns(
grid.Column("Name"),
grid.Column("Given", header: "Date"),
grid.Column("TimeGiven", header: "Time"),
grid.Column("Value"),
grid.Column("Phase"),
grid.Column("PatientId"),
grid.Column("", format: @<text>@Ajax.ActionLink("Disable", "Disable", new { labid = item.Id, patientid = ViewBag.PatientId }, new AjaxOptions { HttpMethod="POST", UpdateTargetId = "labs"})</text>)
)
)
If I remove the ajaxUpdateContainerId from the webgrid constructor the error goes away. But then when I click to sort I get a 404 looking for my controller action. Why would it be looking for my controller action on sort?
The second issue is if I use ajax to modify the partial, in disable, sorting stops working. If I substitue in a html.actionlink sorting continues to work.
I have jquery 1.9.1. Any ideas?