2

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>

The ajax successfully makes a call to the server and the server responds by sending a partial view. But the partial view always renders in a new page. I found out that this is because it doesn't know that it is an ajax call coming and so it renders a new page. I HAVE included the correct jquery-ubobtrusive-ajax.min.js scripts and added the key as follows according to This Question

    <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"> 

My controller code is very simple:

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

      return View("Index", list);

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.

Community
  • 1
  • 1
Dan
  • 215
  • 4
  • 8

5 Answers5

7

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

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1
var controllerContext = MockRepository.GenerateMock<ControllerContext>();
controllerContext.Expect(c => c.HttpContext.Request["X-Requested-With"]).Return("XMLHttpRequest");
controller.ControllerContext = controllerContext;
Chathuranga Chandrasekara
  • 20,548
  • 30
  • 97
  • 138
Ninad
  • 29
  • 5
0

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*"));
Mureinik
  • 297,002
  • 52
  • 306
  • 350
aky
  • 1
0

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

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

jacqijvv
  • 870
  • 6
  • 17