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

后端 未结 8 630
小鲜肉
小鲜肉 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:16

    A simple example, where a change on a dropdown list triggers an ajax form-submit to reload a datagrid:

    <% using (Ajax.BeginForm("UserSearch", "Home", new AjaxOptions { UpdateTargetId = "pnlSearchResults" }, new { id="UserSearchForm" })) { %> UserType: <%: Html.DropDownList("FilterUserType", Model.UserTypes, "--", new { onchange = "$('#UserSearchForm').trigger('submit');" })%> <% } %>

    The trigger('onsubmit') is the key thing: it calls the onsubmit function that MVC has grafted onto the form.

    NB. The UserSearchResults controller returns a PartialView that renders a table using the supplied Model

    <% Html.RenderPartial("UserSearchResults", Model); %>

提交回复
热议问题