2012-02-01 16 views
3

自分自身のラッパーを置き換える@Ajax.ActionLinkを持っています。もう1つのコンテンツは@Ajax.ActionLinkで、最初のhtmlコンテンツをラッパーに返します。それは自己とその逆の部分に取って代わる部分的な部分へのリンクです。私の問題は、ActionLinkへのクリック時にいくつかの追加機能を必要とするとき、私のJqueryは古い部分、クリックされた部分、新しい部分ではなく、Firebugで新しい要素をはっきりと見るが、 OnSuccessとOnCompleteが、しかし成功しなかった!何か案は?Mvc3を取得するAjax.ActionLinkはJqueryでデータを置き換えましたか?

ここでは例です:

VIEW

index.cshtml

<div id="box"> 
    @Html.Partial("_PartialOne") 
</div> 

_ PartialOne.cshtml

@Ajax.ActionLink("Get partial two", "getPartial2", "Home", 
       new AjaxOptions 
        { 
         UpdateTargetId = "box", 
         InsertionMode = InsertionMode.Replace, 
         HttpMethod = "GET"  
        }, 
        new { @class="first"} 
       ) 
<div id="testBox1">Hello, I am partial 1</div> 

_ PartialTwo.cshtml

@Ajax.ActionLink("Get partial one", "getPartial1","Home", 
       new AjaxOptions 
        { 
         UpdateTargetId = "box", 
         InsertionMode = InsertionMode.Replace, 
         HttpMethod = "GET" 
        }, 
        new { @class="second"} 
       ) 

<div id="testBox2">Hello there this is partial 2</div> 

CONTROLLER

HomeController.cs

public PartialViewResult getPartial1() 
{ 
    return PartialView("_PartialOne"); 
} 

public PartialViewResult getPartial2() 
{ 
    return PartialView("_PartialTwo"); 
} 

JS

$("#box a.first").live('click', function() { 

    //how to fetch new data, that was filled with getPartial2() 

    console.log($(this).parent().children()); 

    //this returns [a.first, div#testBox1] 
    // and I need [a.second, div#testBox2] 
}); 

返されたデータを選択して、必要な場合は部分文字列を指定する方法

EDIT onCompleteの= "機能" を使用

VIEW

@Ajax.ActionLink("Get partial two", "getPartial2", "Home", 
       new AjaxOptions 
        { 
         UpdateTargetId = "box", 
         InsertionMode = InsertionMode.Replace, 
         HttpMethod = "GET", 
         OnComplete = "getNewData"  
        }, 
        new { @class="first"} 
       ) 
<div id="testBox1">Hello, I am partial 1</div> 

JS

function getNewData() 
{ 
    console.log($(this)); 

    //this returns [Object] of ajax call, niether 
    // $(this).parent/s(), or $(this).children() is posible 

} 

このajaxと呼ばれる要素の子供を選択するにはどうすればよいですか?

+0

@ダーリンディミトロフ、これをご覧になれますか。 –

+0

jQuery 1.7以降、.live()は推奨されていません。 .on()を使うべきです。 http://api.jquery.com/live/ –

+0

@ZachGreen、ありがとうございます。でも、この問題は解決しませんか? –

答えて

1

あなたが探しているものは100%ではないと思いますが、おそらくあなたの問題はキャッシュされていると思います。 GETリクエストに一意のパラメータを付けると、探している結果が得られますか?

@Ajax.ActionLink("Get partial two", "getPartial2", "Home", new { aparam = DateTime.Now.Ticks }, 
       new AjaxOptions 
        { 
         UpdateTargetId = "box", 
         InsertionMode = InsertionMode.Replace, 
         HttpMethod = "GET", 
         OnComplete = "completeGetThing"  
        }, 
        new { @class="first"} 
       ) 
<div id="testBox1">Hello, I am partial 1</div> 
+0

私の目標は、ActionLinkをトリガしたのと同じクリックでコントローラから返された新しい要素を取得することです。表示する前にそれらを操作することができます。 –

+0

OnCompleteパラメータのjavascript関数を指定すると、アクションが完了したときにその関数が実行されるように設定され、必要なときにページを操作できます。 –

関連する問題