Ajax.ActionLink不工作,Response.IsAjaxRequest()始终是假的 [英] Ajax.ActionLink not working, Response.IsAjaxRequest() is always false

查看:170
本文介绍了Ajax.ActionLink不工作,Response.IsAjaxRequest()始终是假的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在谷歌上搜索/ SO:一会儿荷兰国际集团这个问题,许多人似乎被共享,但我没有找到任何成功的解决我的问题。

I have been googling/SO:ing this issue for a while and many seem to be sharing this, but I haven't found any successful solution to my problem.

使用MVC3和剃刀。

  1. 主网页包含:

  1. Master page contains:

<脚本SRC =@ Url.Content(〜/脚本/ jQuery的-1.5.min.js)类型=文/ JavaScript的>< / SCRIPT>

<脚本SRC =@ Url.Content(〜/脚本/ MicrosoftAjax.js)类型=文/ JavaScript的>< / SCRIPT>

<脚本SRC =@ Url.Content(〜/脚本/ MicrosoftMvcAjax.js)类型=文/ JavaScript的>< / SCRIPT>

AjaxTest.cshtml包括:

AjaxTest.cshtml contains:

< D​​IV ID =AjaxTestDiv>含量小于/ DIV>

@ Ajax.ActionLink(更新,AjaxTester,新AjaxOptions {UpdateTargetId =AjaxTestDiv})

AjaxTester操作方法:

AjaxTester action method:

public string AjaxTester()
{
    if (Request.IsAjaxRequest())
    {
        return DateTime.Now.ToString();
    }
    else
    {
        return "FAIL";
    }
}

我总是得到FAIL回来,到一个空白页,而不是在有针对性的DIV。

I always get the "FAIL" returned, to a blank page, not in the targeted div.

编辑:还要注意的是,如果我删除,如果(Request.IsAjaxRequest()),我仍然没有找回任何有针对性的股利,而是一个空白页

Also note that if I remove the if (Request.IsAjaxRequest()), I still don't get back anything to the targeted div, but instead a blank page.

EDIT2:纵观生成的HTML,这是我的链接:

Looking at the HTML generated, this is my link:

<a data-ajax="true" data-ajax-method="POST" data-ajax-mode="replace"
data-ajax-update="#AjaxTestDiv" href="/Area/AjaxTester">Update</a>

试过切换方法来获得,但没有成功。

Have tried switching the method to GET, to no avail.

推荐答案

在默认情况下ASP.NET MVC 3使用不显眼的jQuery的所有的阿贾克斯。* 帮手。因此,通过摆脱了所有MicrosoftAjax脚本(这个没用C ** p)的启动,把下面来代替:

By default ASP.NET MVC 3 uses unobtrusive jquery with all the Ajax.* helpers. So start by getting rid off all MicrosoftAjax scripts (this useless c**p) and put the following instead:

<script src="@Url.Content("~/Scripts/jquery-1.5.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

然后只需激活不显眼的AJAX在你的web.config(如果尚未完成):

and then simply activate unobtrusive AJAX in your web.config (if not already done):

<appSettings>
    <add key="ClientValidationEnabled" value="true"/> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/> 
</appSettings>

现在jQuery是将含有这些HTML 5 数据来悄悄地AJAXify的所有环节 - * 属性

Now jquery is going to unobtrusively AJAXify all the links containing those HTML 5 data-* attributes.

甚至更好恕我直言:

在你看来简单:

@Html.ActionLink("Update", "AjaxTester", new { id = "mylink" })

在一个单独的JavaScript文件AJAXify这种定位的:

and in a separate javascript file AJAXify this anchor:

$(function() {
    $('#mylink').click(function() {
        $('#AjaxTestDiv').load(this.href);
        return false;
    });
});

这篇关于Ajax.ActionLink不工作,Response.IsAjaxRequest()始终是假的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆