Asp.net mvc 从Ajax调用填充Webgrid
我在通过Ajax调用填充Webgrid时遇到问题 我遵循了以下线程中显示的示例:但是,这并没有产生任何结果 当我运行网站时,我总是收到这样的信息:“错误:未定义” 在调试代码时,我非常确定问题在于返回PartialView是问题所在,因为ajax成功方法中的数据对象没有填充数据 以下是我的代码示例: Ajax调用:Asp.net mvc 从Ajax调用填充Webgrid,asp.net-mvc,asp.net-mvc-3,jquery,Asp.net Mvc,Asp.net Mvc 3,Jquery,我在通过Ajax调用填充Webgrid时遇到问题 我遵循了以下线程中显示的示例:但是,这并没有产生任何结果 当我运行网站时,我总是收到这样的信息:“错误:未定义” 在调试代码时,我非常确定问题在于返回PartialView是问题所在,因为ajax成功方法中的数据对象没有填充数据 以下是我的代码示例: Ajax调用: $.fn.getCardResult = function (leerling, kaart) { $.ajax({ type: "GET",
$.fn.getCardResult = function (leerling, kaart) {
$.ajax({
type: "GET",
url: '@Url.Action("GetResults","Kaarten")',
data: { leerlingID: leerling, cardID: kaart },
cache: false,
success: function (data) {
console.log(data);
if (!data.ok) {
window.alert(' error : ' + data.message);
}
else {
$('#card').html(data);
}
}
});
}
局部视图调用:
<div class="card-content" id="card">
@{
if(Model.Kaart != null && Model.Kaart.Count > 0)
{
@Html.Partial("_Kaarten")
}
else
{
@: Er zijn geen kaarten beschikbaar.
}
}
</div>
我找到的解决办法是用另一种方法来实现成功。这样可以确保正确渲染局部视图。下面是Ajax调用片段
$.ajax({
url: '@Url.Action("GetAjaxCall","Home")',
contentType: 'application/html; charset=utf-8',
type: 'GET',
dataType: 'html',
data: { id: id },
})
.success(function (result) {
$('#sectionContents').html(result);
})
.error(function (xhr, status) {
alert(xhr.responseText);
});
$.ajax({
url: '@Url.Action("GetAjaxCall","Home")',
contentType: 'application/html; charset=utf-8',
type: 'GET',
dataType: 'html',
data: { id: id },
})
.success(function (result) {
$('#sectionContents').html(result);
})
.error(function (xhr, status) {
alert(xhr.responseText);
});
如果是AJAX调用,那么您应该返回一个JSON对象,而不是PartialView<代码>返回Json(新的{@data=KVM.Kaart},JsonRequestBehavior.AllowGet)代码>嗯,我发现了问题,但不是这样。很明显,你不想有一个成功的方法,就像我最初做的那样。我将用新的Ajax调用更新我的问题。如果你已经解决了你的问题,帖子会自己给出答案
$.ajax({
url: '@Url.Action("GetAjaxCall","Home")',
contentType: 'application/html; charset=utf-8',
type: 'GET',
dataType: 'html',
data: { id: id },
})
.success(function (result) {
$('#sectionContents').html(result);
})
.error(function (xhr, status) {
alert(xhr.responseText);
});
$.ajax({
url: '@Url.Action("GetAjaxCall","Home")',
contentType: 'application/html; charset=utf-8',
type: 'GET',
dataType: 'html',
data: { id: id },
})
.success(function (result) {
$('#sectionContents').html(result);
})
.error(function (xhr, status) {
alert(xhr.responseText);
});