2012-02-01 35 views
3

我有一個@Ajax.ActionLink替換他自己的包裝,另一個內容也有@Ajax.ActionLink,返回第一個html內容包裝。這是部分鏈接,取代它的自我和反之亦然。我的問題是,當我需要點擊動作鏈接時,我的jquery返回舊部分,已經點擊,而不是新的,在螢火蟲我清楚地看到我的新元素,但無法達到他們,我試過了, 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> 

控制器

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] 
}); 

如何選擇返回的數據,讓我們說子字符串,如果neccesary?

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

@Darin Dimitrov,你可以看看這個... – 2012-02-01 13:59:25

+0

作爲jQuery 1.7,.live()已被棄用。你應該使用.on()。 http://api.jquery.com/live/ – 2012-02-01 14:00:00

+0

@ZachGreen,謝謝,但這並不能解決這個問題? – 2012-02-01 14:06:54

回答

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的相同點擊,所以我可以在顯示之前操縱它們。 – 2012-02-01 18:01:04

+0

爲OnComplete參數提供一個javascript函數將設置該函數在動作完成時執行,然後您可以根據需要操作頁面。 – 2012-02-01 18:11:45