views:

1615

answers:

3

This MVC stuff is fun but every step along the way theres another little hurdle.

I'm just using Request.IsAjaxRequest for the first time today in conjunction with Microsoft's AJAX library. I am NOT yet using jQuery (I had to mention that since I just hyperlinked to a question about jQuery!).

Unfortunately I installed RC1 refresh BEFORE I tested this code.

I am using Ajax.BeginForm.

<% using (Ajax.BeginForm("Contact", "AboutUs", new AjaxOptions()
      {
          OnBegin = "submitComments_begin",
          OnSuccess = "submitComments_success",
          OnFailure = "submitComments_failure",
          OnComplete = "submitComments_complete",
          LoadingElementId = "submitting"

      }, new { id="fooForm" }))

When my controller action executes these are my headers :

Connection Pragma, Content-Length, Content-Type, Accept, Accept-Encoding, Accept-Language, Host, Referer, User-Agent, UA-CPU

In addition the FORM contains this Param

Request["__MVCASYNCPOST"] = "true"

And the end effect is that this returns false!

Request.IsAjaxRequest()

I get the issue with Chrome and Internet Explorer.

Please tell me if I'm doin something silly - or if something just broke.

Final thought: Hmm - perhaps I need a new futures DLL. I'll update if that turns out to be the issue

A: 

I'm still not sure if this is a bug or not - can anyone confirm?? Remember I'm talking about the new RC1 Refresh, not the original RC1.

In the event that it IS a bug (and I'm hoping i'm just doing something careless) then this is my temporary solution for now:

Extension method on HttpRequestBase :

public static bool IsAjaxRequest2(this HttpRequestBase request)
        {
            return !string.IsNullOrEmpty(
                   request.Params["__MVCASYNCPOST"] as string);
        }

Then I just use Request.IsAjaxRequest2() and I'll search and replace later when its fixed.

Simon_Weaver
+1  A: 

Thanks to Phil for telling me I had to update the .js file MicrosoftMvcAjax.js. Even though it didn't actually tell me in the release notes that I need to update them.

Phil: If you want the points for the correct answer you have 2 days until i can accept my own answer. Thanks!

Simon_Weaver
A: 

It doesn't show in the snippet you have posted, but you most likely have form /form tags. These are interfering with the Ajax.BeginForm which generates the form tags as well. Once you remove the outer form tags, the Request.IsAjaxRequest will return true.

David Neises
that might be a problem elsewhere, but in this case it was the MicrosoftMvcAjax.js that needed updating. i've switched to jQuery now anyway
Simon_Weaver