【问题标题】:Get Mvc3 Ajax.ActionLink replaced data with Jquery?用 Jquery 获取 Mvc3 Ajax.ActionLink 替换数据?
【发布时间】:2012-02-01 11:08:09
【问题描述】:

我有一个@Ajax.ActionLink 替换他自己的包装器,另一个内容也有@Ajax.ActionLink,它将第一个html 内容返回给包装器。它是部分链接,它取代了它的自我,反之亦然。我的问题是,当我想在点击 ActionLink 时获得一些额外的功能时,我的 Jquery 返回旧的部分,即已点击的部分,而不是新的部分,在萤火虫中我清楚地看到了我的新元素,但无法访问它们,我试过了, 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>

控制器

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

【问题讨论】:

  • @Darin Dimitrov,你能看看这个...
  • 从 jQuery 1.7 开始,.live() 已被弃用。您应该使用 .on()。 api.jquery.com/live
  • @ZachGreen,谢谢,但这并不能解决这个问题?
  • 这就是为什么我把它作为评论而不是答案
  • @ZachGreen,有什么答案吗?

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


【解决方案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>

【讨论】:

  • 我的目标是让控制器返回新元素,点击触发 ActionLink 的相同点击,这样我就可以在显示之前对它们进行操作。
  • 为 OnComplete 参数提供 javascript 函数将设置该函数在操作完成时执行,然后您可以根据需要操作页面。
猜你喜欢
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多