asp.net-mvc - Asp.net Mvc jquery ajax?

标签 asp.net-mvc jquery partial-views

我有如下链接。

<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/

相关文章:

asp.net-mvc - 在 Controller 或其他地方将部分渲染为字符串

ruby-on-rails-3 - Rails 部分有时需要太多时间来渲染

javascript - HTML Canvas 大小调整为 0px

javascript:页面加载后执行该功能的最佳方式

javascript - html 页面中的 Ajax 谷歌分析事件跟踪不起作用

jquery - 在就绪处理程序中使用字符串调用函数...不起作用

asp.net-mvc - 可以在多个mvc 3项目中共享 View

asp.net-mvc - 条件模型绑定(bind)

asp.net-mvc - 在 ASP.NET MVC 的 View 层中使用 webservice 类型好吗?

asp.net - 如何修复为 Microsoft.aspnetcore.razor.language 检测到的版本冲突?