1

次のようなリンクがあります。

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

私は次のようなjqueryajax投稿を使用したいと思います:

$(".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="#">と、機能します。

ありがとう。

4

4 に答える 4

1

問題は、リンクをクリックすると、すでにナビゲーションを開始していることです。したがって、デフォルトの動作を防ぐには、メソッドを使用するe.preventDefault()return false、メソッドから使​​用してくださいclick

$(".customer_details").click(function (e) {
    e.preventDefault();
    ...
}
于 2012-11-27T08:09:57.890 に答える
1

これを試して

$(function(){
    $(".customer_details").click(function (e) {
        e.preventDefault();
    });
});

readyイベントを使用する

デモ: http: //jsfiddle.net/hdqDZ/

于 2012-11-27T08:33:06.617 に答える
1

問題はアクション結果にある可能性があります。コンテンツを試して、何かが変わるかどうかを確認してください。

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 エラーがないかどうかを確認してください

于 2012-11-27T08:19:41.570 に答える
1

これはあなたを助けるはずです:

            $.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
            return false;

あなたが欠けている唯一のものは、Aタグが機能するのを防ぐことです. カスタムイベントを返すfalseと、デフォルトのイベントは実行されません。

于 2012-11-27T08:22:55.467 に答える