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");
});
-
What does call action result method means ? You want to submit your form data ?Shyju– Shyju2016-07-30 17:16:05 +00:00Commented Jul 30, 2016 at 17:16
-
Yes to submit data with Jquery calling ActionResult Create Method ?jhony3– jhony32016-07-30 17:23:48 +00:00Commented Jul 30, 2016 at 17:23
-
See the answer i posted.Shyju– Shyju2016-07-30 17:24:53 +00:00Commented Jul 30, 2016 at 17:24
Add a comment
|
1 Answer
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" />
}
3 Comments
jhony3
Thx @Shyju I will try yes my button is inside the form and my form action is set on Create
jhony3
Sorry @Shyju do you know how can I redirect from this code to another page after inserting ?
Shyju
Replace the
//do something with response inside the $.post success callback with with window.location.href='/Home/Index';