【问题标题】:Checking Request.IsAjaxRequest always return false inside my asp.net mvc4检查 Request.IsAjaxRequest 在我的 asp.net mvc4 中总是返回 false
【发布时间】:2013-07-05 12:31:26
【问题描述】:

我的控制器中有以下代码

public ActionResult Index(string searchTerm=null)
        {   System.Threading.Thread.Sleep(5000);
            var accountdefinition = repository.FindAccountDefinition(searchTerm).ToList();
            if (Request.IsAjaxRequest())
            { return PartialView("_CustomerTable",accountdefinition); }
        return View(accountdefinition);         
        }

但是如果我使用 Ajax.beginform 调用上述操作方法,那么 Request.IsAjaxRequest 将返回 false,并且不会返回部分视图

@using (Ajax.BeginForm(
new AjaxOptions{
    HttpMethod= "get",
    InsertionMode=InsertionMode.Replace,
    LoadingElementId = "progress",
    UpdateTargetId="customerTable"}))
    {
<div style="float:right">Search <input placeholder="Search by name.." name="searchTerm" type="text"> <input class="btn btn-success" type="submit" value="search" /></div>               

}
<div id = "progress" class="loadingimage">
<img src="~/Content/Ajax-loader-bar.gif" />
</div>

【问题讨论】:

  • 试试this.ControllerContext.HttpContext.Request.IsAjaxRequest()

标签: c# asp.net-mvc


【解决方案1】:

在我看来,我没有包含 jquery.unobtrusive-ajax.min.js

【讨论】:

  • 这是在哪里记录的?
  • @john G:您应该将此标记为答案。这行得通。
  • 如果我不使用 jQuery 怎么办??
【解决方案2】:

我遇到了这个问题,不确定 jquery.unobtrusive-ajax.min.js 文件在做什么黑魔法,但它对我有用。当我遇到这个post 时我很高兴,它解释了这个非常简单的问题。

帖子的作者声明有一个需要填充的标题。

X-Requested-With =&gt; 'XMLHttpRequest'

对于找到这篇文章的 Angular 用户,我在post 上方添加了一个 sn-p。

var productsApp = angular.module('productsApp', []);

productsApp.config(['$httpProvider', function ($httpProvider) {
  $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
}]);

【讨论】:

    【解决方案3】:

    你好,试试这个,像这样改变你的观点,希望它对你有用

     @using (Ajax.BeginForm("Index", "controller", null, new AjaxOptions { HttpMethod = "post", InsertionMode = InsertionMode.Replace, LoadingElementId = "progress", UpdateTargetId = "customerTable" }))
            {
                <div style="float: right">
                    Search
                    <input placeholder="Search by name.." name="searchTerm" type="text">
                    <input class="btn btn-success" type="submit" value="search" /></div>               
    
            }
    

    【讨论】:

      猜你喜欢
      • 2015-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-13
      • 2017-08-02
      • 2021-06-24
      相关资源
      最近更新 更多