Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net MVC Ajax.BeginForm is not submitting via Ajax

I've got my form as follows

<div id="contact-form" class="hidden" title="Online Request Form">
    @Using (Ajax.BeginForm("Contact", "Main",
                          Nothing,
                          New AjaxOptions With {.UpdateTargetId = "status", .HttpMethod = "post"},
                          New With {.id = "contactUs"}))
        @<div>
            @Html.LabelFor(Function(m) m.Name)<br />
            @Html.TextBoxFor(Function(m) m.Name)<br />
            @Html.LabelFor(Function(m) m.Phone)<br />
            @Html.TextBoxFor(Function(m) m.Phone)<br />
            @Html.LabelFor(Function(m) m.Email)<br />
            @Html.TextBoxFor(Function(m) m.Email)<br />
            @Html.LabelFor(Function(m) m.Question)<br />
            @Html.TextAreaFor(Function(m) m.Question)<br />
            @Html.LabelFor(function(m) m.Security)<br />
            @Html.TextBoxFor(Function(m) m.Security)<br />
            <noscript>
                <input type="submit" name="submit" value="Ok" />
            </noscript>
            @Html.ValidationSummary("Oops, please correct the errors.")<span id="status">@TempData("status")</span>
        </div>
    End Using
</div>

And I'm opening it in a jQuery-UI Modal Window

<script>
    $(function () {

        // Open the modal dialog from the div.contact-us click event
        $('#contact-us').click(function () {
            $('#contact-form').dialog('open');
            return false;
        });

        // Manage the modal dialog behavior.
        $('#contact-form').dialog({
            modal: true,
            autoOpen: false,
            buttons: {
                Cancel: function () {
                    $(this).dialog('close');
                },
                Ok: function () {
                    $('form#contactUs').trigger('submit');
                }
            }
        });
    });

</script>

When I click the "OK" button, it is posting to the appropriate controller, however it is not posting via AJAX

    ''# fix the StackOverflow code coloring issue.
    <HttpPost()>
    Function Contact(ByVal contactForm As Models.ContactForm) As ActionResult
        ViewData("Testimonials") = Helpers.GetTestimonials

        If ModelState.IsValid Then
            ''# Submit the email
            TempData("status") = "Thank you, we will be in touch"
        Else
            ''# Return False
            TempData("status") = "Oops, please correct the errors."
        End If


        If Request.IsAjaxRequest Then
            Return Content(TempData("status").ToString)
        Else
            Return View("Index")
        End If
    End Function

What am I doing wrong? After I submit the form, my URL is http://example.com/Main/Contact which tells me that IsAjaxRequest = false

EDIT

Even when I don't use the jquery-ui "ok" button and simply add <input type="submit" name="submit" value="Ok" /> to the form, the form posts without Ajax

like image 534
Chase Florell Avatar asked Dec 18 '10 03:12

Chase Florell


People also ask

What is difference between Html BeginForm and Ajax BeginForm?

BeginForm() will create a form on the page that submits its values to the server as a synchronous HTTP request, refreshing the entire page in the process. Ajax. BeginForm() creates a form that submits its values using an asynchronous ajax request.

What is Ajax BeginForm?

Ajax. BeginForm is the extension method of the ASP.NET MVC Ajax helper class, which is used to submit form data to the server without whole page postback. To work Ajax. BeginForm functionality properly, we need to add the reference of jquery.

What is@ using Html BeginForm()) in MVC?

Html. BeginForm is the Html Helper Extension Method that is used for creating and rendering the form in HTML. This method makes your job easier in creating form. Here, is the method to create a form using Html.

How does Html BeginForm work?

It automatically binds each input control (by name) to the parameters of the action accepting the postback - or in your case, the properties of the object parameter for the action accepting the postback.


1 Answers

Do you have the jquery ajax script included? I've noticed this sort of behavior when I didn't include it:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
like image 154
Jeff Ogata Avatar answered Oct 14 '22 15:10

Jeff Ogata