jhony3
jhony3

Reputation: 272

How can I prevent submit event and with onclick event call ActionResult Method Create to submit my form data using Jquery ASP.NET MVC?

How can I call action result method using JQuery with onclick event if my button type is submit I tried like this $("#btnSave).click(function(e){ e.preventDefault(); @Url.Action("Create","ControllerName"); });

Upvotes: 2

Views: 1107

Answers (1)

Shyju
Shyju

Reputation: 218732

If you want to submit your form data using ajax, you may use the jQuery serialize() method to serialize your form and send that.

$(function(){

  $("#btnSave").click(function(e){
      e.preventDefault();
      var _f=$(this).closest("form");
      $.post(_f.attr("action"),_f.seriazlize(),function(response){
         //do something with response
      });
  });

});

Assuming your button is inside the form which you want to submit and it has the action attribute value set to Create action method.

@using(Html.BeginForm("Create","YourControllerName"))
{
  <!-- Your other form elements goes here -->
  <input type="button" id="btnSave" />
}

Upvotes: 3

Related Questions