Request.IsAjaxRequest never returns true in MVC3

后端 未结 5 546
眼角桃花
眼角桃花 2021-01-15 14:22

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.

    @usi         


        
相关标签:
5条回答
  • 2021-01-15 14:44
    var controllerContext = MockRepository.GenerateMock<ControllerContext>();
    controllerContext.Expect(c => c.HttpContext.Request["X-Requested-With"]).Return("XMLHttpRequest");
    controller.ControllerContext = controllerContext;
    
    0 讨论(0)
  • 2021-01-15 14:53

    Make sure that the jquery.unobtrusive.ajax.js / jquery.unobtrusive.ajax.min.js script file is referenced after the other jquery script files, also make sure that you dont reference the file more than once per page and you should be sorted

    0 讨论(0)
  • 2021-01-15 14:53

    You need to add the unobtrusive script after jquery script like this.

    bundles.Add(new ScriptBundle("~/bundles/searchDemo").Include(
                    "~/Scripts/jquery-{version}.js",
                    "~/Scripts/jquery-ui-{version}.js",
                    "~/Scripts/jquery.unobtrusive*",
                    "~/Scripts/jquery.validate*"));
    
    0 讨论(0)
  • 2021-01-15 14:58

    I stumbled on the similar situation where IsAjaxRequest() didn't work. I made sure Microsoft unobtrusive package is included. It still didn't help. Just upgraded to the latest version of unobtrusive library from package manager and it worked!

    0 讨论(0)
  • 2021-01-15 15:01

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

    0 讨论(0)
提交回复
热议问题