Request.IsAjaxRequest从来没有在MVC3返回true [英] Request.IsAjaxRequest never returns true in MVC3

查看:306
本文介绍了Request.IsAjaxRequest从来没有在MVC3返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Asp.net MVC3,并尝试做一个简单的Ajax职位,它返回一个局部视图,并在搜索过程中更新我的产品清单的服务器。

I am using Asp.net MVC3 and trying to do a simple Ajax post to the server which returns a partial view and updates my list of items during a search.

    @using (Ajax.BeginForm("PartialUpdate", "Listing"
        , new  AjaxOptions { 
                UpdateTargetId = "ListPartialDiv",
                HttpMethod = "GET",
                InsertionMode = InsertionMode.Replace,

        }))
    {
        <!-- Search Boxes and buttons here -->
    }

    <div id = "ListPartialDiv">
        @{
            Html.RenderPartial("_ListPartial", Model);
         }
    </div>

阿贾克斯成功地将调用服务器和服务器发送一个局部视图来响应。但局部视图总是在一个新的页面呈现。我发现,这是因为它不知道这是一个Ajax调用来了,所以它呈现了新的一页。我已经包含了正确的jQuery ubobtrusive-ajax.min.js脚本,并且增加了密钥,根据如下<一个href="http://stackoverflow.com/questions/5164540/why-doesnt-request-isajaxrequest-work-in-asp-net-mvc-3">This问

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

我的控制器code是很简单的:

My controller code is very simple:

      if (Request.IsAjaxRequest())
          return PartialView("_ListPartial", list);

      return View("Index", list);

但Request.IsAjaxRequest()总是不管返回false。 根据该课题并没有看到在X请求 - 随着头,但我真的不能找出这意味着什么或如何去改变它。我唯一​​能想到的是,我看到每一个其他例子使用的jQuery的旧版本。这是否有什么关系呢?感谢您的帮助。

But Request.IsAjaxRequest() always returns false no matter what. I have also used fiddler and firebug according to This Question and don't see the X-Requested-With header but I can't really figure out what that means or how to change it. The only thing I can think of is that every other example I see uses an older version of jquery. Does that have anything to do with it? Thanks for any help.

推荐答案

您正在包括不显眼,AJAX脚本的jQuery之前,所以没有脚本的工作。

You're including the unobtrusive-AJAX script before jQuery, so none of your scripts are working.

这篇关于Request.IsAjaxRequest从来没有在MVC3返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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