我有如下链接。
<a href="/Customer/_EditCustomer?CustomerId=2" class="customer_details">Deneme Müşteri 2</a>
<a href="/Customer/_EditCustomer?CustomerId=3" class="customer_details">Deneme Müşteri 2</a>
我想像这样使用jquery ajax post:
$(".customer_details").click(function () {
$.ajax({
url: $(this).attr("href"),
type: 'POST',
beforeSend: function () {
},
complete: function () {
},
success: function (result) {
$("#customer_operations_container").html(result);
},
error: function (result) {
alert("Hata!");
}
}); //end ajax
});
或者这个:
$(".customer_details").click(function () {
$("#customer_operations_container").load($(this).attr("href"));
});
及操作方法
public ActionResult _EditCustomer(int CustomerId)
{
// get customer from db by customer id.
return PartialView(customer);
}
但我不能做我想做的事。当我单击链接时,PartialView 不会加载。它作为一个新页面打开,没有其父页面。我尝试了 Prevent.Default 但结果是相同的。
如何将partialView加载到div中?
注意:如果我使用这样的链接 <a href="#">
它有效。
谢谢。
最佳答案
也许问题出在 actionresult 上,尝试使用 Content 看看是否会改变任何内容。
public ActionResult _EditCustomer(int CustomerId)
{
// get customer from db by customer id.
return Content(customer.ToString());
}
尝试其中之一...
$(".customer_details").click(function (e) {
e.preventDefault()
$.ajax({
url: $(this).attr("href"),
//I think you want a GET here? Right?
type: 'GET',
beforeSend: function () {
},
complete: function () {
},
success: function (result) {
$("#customer_operations_container").html(result);
},
error: function (result) {
alert("Hata!");
}
}); //end ajax
});
或者
$(".customer_details").click(function (e) {
e.preventDefault();
$("#customer_operations_container").load($(this).attr("href"));
});
或者
$(".customer_details").click(function (e) {
e.preventDefault();
$.get($(this).attr("href"), function(data) {
$("#customer_operations_container").html(data);
});
});
如果这些都不起作用,请检查是否有任何js错误
关于asp.net-mvc - Asp.net Mvc jquery ajax?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13579456/