Request.IsAjaxRequest()总是返回在MVC4假的,试图SO,等所有建议 [英] Request.IsAjaxRequest() always returning false in MVC4, tried all suggestions in SO, etc

查看:87
本文介绍了Request.IsAjaxRequest()总是返回在MVC4假的,试图SO,等所有建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,让我解释所有可能的解决办法我试过开始。我现在有 jQuery的,不显眼,ajax.min.js 里面我的脚本文件夹,并把它添加到包。我试着引用它本身视图页面上,也与在 _Layout.cshtml 页以来,这是我目前有捆绑:

Let me start off by explaining all the possible solutions I've tried. I now have jquery-unobtrusive-ajax.min.js inside my scripts folder and have added it to a bundle. I've tried referencing it on the view page itself, along with the _Layout.cshtml page also, this is how I have the bundle currently:

//BundleConfig.cs
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
            "~/Scripts/jquery-1.10.2.min.js",
            "~/Scripts/modernizr-2.6.2.js",
            "~/Scripts/jquery.validate.min.js",
            "~/Scripts/jquery.unobtrusive-ajax.min.js"));

这捆在我的所有其他的意见都源于主布局视图页面引用的:

This bundle is referenced in the main layout view page that all my other views are derived from:

//_Layout.cshtml (at bottom of view, right before hitting </body></html>)
@Scripts.Render("~/bundles/jquery")

最后在web.config中,我已经添加了这些键的应用程序设置:

Lastly in web.config, I've added these keys to the app settings:

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

既然已经指出,这是我的Ajax调用的样子。我试图从一个独立的页面,什么是在主页面相当简单我想返回一些数据,并替换:

Now that that has been stated, this is what my Ajax call looks like. I'm trying to return some data from a stand alone page and replace it with what's in the main page, fairly straightforward I would think:

@Ajax.ActionLink("Edit", "Index", "WeeklyTarget",
    new {id = item.WeeklyTargetId},
    new AjaxOptions {HttpMethod = "GET", UpdateTargetId = "hoursEdit", InsertionMode = InsertionMode.Replace})

在这个动作链接被点击构成索引视图这是从控制器名为:

When this action link is clicked form the index view this is called from the controller:

public ActionResult Index(int? id, IEnumerable<WeeklyTarget> selection )
{            
    if (Request.IsAjaxRequest())
    {
    // return the partial view here
    }
}

但正如我的标题所说, Request.IsAjaxRequest()仍然会始终返回false。是什么赋予了???请帮助...我已经用尽了所有的思想,调整和变通我可能想到的。我甚至尝试清理出我的缓存,清洁液等。

But as stated in my title, Request.IsAjaxRequest() will still always return false. What gives??? Please help... I've exhausted all ideas, tweaks, and work arounds I could possibly think of. I even tried clearing out my caches, cleaning solutions, etc.

推荐答案

所以......问题是, jQuery的,不显眼,ajax.min.js 被引用德precated jQuery的方法 .live()。我能看到这是在Chrome的开发者控制台被抛出,因为我是调试错误。如果你使用jQuery的版本1.9过去(我用的是1.10.2)这才会发生。据德precated在1.7,但1.9完全删除。

So... the issue was that jquery-unobtrusive-ajax.min.js was referencing the deprecated jQuery method .live(). I was able to see this as an error being thrown in chrome's developer console as I was debugging. This will only happen if you use versions of jQuery past 1.9 (I was using 1.10.2). It was deprecated in 1.7 but removed completely in 1.9.

解决的办法是在另一个jQuery库添加 jQuery的-迁移-1.2.1.js可以在这里找到:<一href=\"https://github.com/jquery/jquery-migrate#readme\">https://github.com/jquery/jquery-migrate#readme更具体地说(用于开发,而不是生产的目的)位置:<一href=\"http://$c$c.jquery.com/jquery-migrate-1.2.1.js\">http://$c$c.jquery.com/jquery-migrate-1.2.1.js.我保存,包括在我的 BundleConfig.cs 脚本这个库,其余开始担任意。我希望这会帮助别人。

The solution was to add in another jquery library jquery-migrate-1.2.1.js which can be found here: https://github.com/jquery/jquery-migrate#readme and more specifically (for development, not production purposes) here: http://code.jquery.com/jquery-migrate-1.2.1.js. I saved and included this library in my BundleConfig.cs scripts and the rest began working as intended. I hope this will help others.

这篇关于Request.IsAjaxRequest()总是返回在MVC4假的,试图SO,等所有建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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