views:

104

answers:

2

My question is similar to this one and I am having a similar issue, but a big difference is that I'm using the Ajax helper methods (Ajax.ActionLink and Ajax.BeginForm) instead of handling the AJAX with jQuery.

Request.IsAjaxRequest() is returning true for the Edit method that accepts http GET, but false for Edit method accepting http POST.

The GET request comes from a link generated by:

<%=Ajax.ActionLink(item.Name, "Edit", "Device",
     new { id = item.ID },
     new AjaxOptions { HttpMethod= "GET", UpdateTargetId = "ModalDialog" },
     new { name = item.Name })%>

The POST request comes from a form generated by this code:

<% using (Ajax.BeginForm("Edit", "Device", new { id = Model.ID }, new AjaxOptions { OnComplete = "CloseDialog" }))
{ %>
    <fieldset>
         <h4>
            <label for="Name">Name</label>
        </h4>
        <%= Html.TextBox("Name", null, new { @class = "required" })%>

        <h4>
            <input type="checkbox" id="IsActive" name="IsActive" <% if (Model.IsActive)%> <%=Html.Encode("checked=''")%> />
            <label for="IsActive">Unit Is Active</label>
        </h4>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
<% } %>

Is this by design, am I doing something wrong, and how do I fix this?

A: 

I am pretty surprised to hear that. You can install the Firebug addon for Firefox, and monitor network traffic from the NET tab. Only aSync requests will show up in the XHR sub-tab.

Josh Pearce
As expected, the GET request shows up while the POST request does not. Any ideas why this may be?
oltman
Can you post the contents of your form?
Josh Pearce
Form contents posted. I don't seen anything in here that could be causing a problem, but I guess that's why I'm here in the first place :)
oltman
No clue... I don't suppose the page is available on the WWW somewhere, is it?
Josh Pearce
Nope. I think there may be an issue with `OnComplete = "CloseDialog"` that's causing it to fall back to a synchronous call.
oltman
+1  A: 

Everything there looks fine. One thought: in your AjaxOptions you specify a "CloseDialog" function for OnComplete. Has that been created and is it accessible to the form? If not, the MvcAjax script will throw an error and the form will revert to a regular postback.

Nate
Good call. I had the "CloseDialog" in the `(document).ready(function() {` of the page.
oltman
Any idea why it reverts to a regular postback instead of throwing some kind of exception or letting the developer know what's going on in some way?
oltman
It does throw an exception but its a js exception so, client-side. If you put a few breakpoints in the MvcAjax file, you'll see the exception thrown. Once the error occurs, the browser takes over and submits the form like any other form.
Nate
All very good to know. Thanks!
oltman