How to post ASP.NET MVC Ajax form using JavaScript rather than submit button

后端 未结 8 636
小鲜肉
小鲜肉 2020-11-30 23:20

I have a simple form created using Ajax.BeginForm:

<% using (Ajax.BeginForm(\"Update\", \"Description\", new { id = Model.Id         


        
8条回答
  •  生来不讨喜
    2020-12-01 00:21

    Unfortunately triggering the onsubmit or submit events wont work in all browsers.

    • Works in IE and Chrome: #('form#ajaxForm')trigger('onsubmit');
    • Works in Firefox and Safari: #('form#ajaxForm')trigger('submit');

    Also, if you trigger('submit') in Chrome or IE, it causes the entire page to be posted rather than doing an AJAX behavior.

    What works for all browsers is removing the onsubmit event behavior and just calling submit() on the form itself.

    
      <% using (Ajax.BeginForm("Update", "Description", new { id = Model.Id },
         new AjaxOptions
         {
           UpdateTargetId = "DescriptionDiv",
           HttpMethod = "post"
         }, new { id = "ajaxForm" } )) {%>
       Description:
       <%= Html.TextBox("Description", Model.Description) %>
    Save <% } %>

    Also, the link doesn't have to be contained within the form in order for this to work.

提交回复
热议问题