获取Mvc3 Ajax.ActionLink用Jquery替换数据?

时间:2012-02-01 11:08:09

标签: c# jquery .net ajax asp.net-mvc-3

我有一个@Ajax.ActionLink替换他自己的包装器,另一个内容也有@Ajax.ActionLink,它将第一个html内容返回给包装器。它是部分的链接,取代了它的自我和反之亦然。我的问题是当我想要一些aditional functionaliti点击ActionLink时,我的Jquery返回旧的部分,已经点击的那个,而不是新的,在firebug中我清楚地看到我的新元素,但是无法触及它们,我试过, OnSuccess和OnComplete,但没有成功!有什么想法吗?

以下是示例:

查看

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

如何选择返回的数据让我们说必要的子串?

修改

使用OnComplete =“function”

查看

@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的元素的子元素?

1 个答案:

答案 0 :(得分: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>