2

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"); });

3
  • What does call action result method means ? You want to submit your form data ? Commented Jul 30, 2016 at 17:16
  • Yes to submit data with Jquery calling ActionResult Create Method ? Commented Jul 30, 2016 at 17:23
  • See the answer i posted. Commented Jul 30, 2016 at 17:24

1 Answer 1

3

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" />
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thx @Shyju I will try yes my button is inside the form and my form action is set on Create
Sorry @Shyju do you know how can I redirect from this code to another page after inserting ?
Replace the //do something with response inside the $.post success callback with with window.location.href='/Home/Index';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.