c# - Jquery form submit() to the action in the controller then returns an object to the client and not reload page -
i want after call submit() function in jquery. go action process logic, action return object client, client displays it.
i tried many ways no, please me.
i have form this:
<form action="/mycontroller/save" id="myform" enctype="multipart/form-data" method="post"> first name:<br> <input type="text" name="firstname" value="mickey"> <br> last name:<br> <input type="text" name="lastname" value="mouse"> <br><br> </form> <button class="btn btn-success" type="button" onclick="save()">save</button>
i want try code js:
function save( $("#myform").submit(function (eventdata, handler) { var resultfromaction = ??? // object action returned }); )
or
function save( var resultfromaction = $("#myform").submit(); // object action returned )
code action in controller:
public class mycontrollercontroller: controller { [authorize] public actionresult save(formobject formobj) { // var resultforclient = new { phone: "098989878", name: "john", add: "my address" }; return json(resultforclient, jsonrequestbehavior.allowget); } }
firstly should place <button>
within <form>
, change type
submit
:
<form action="/mycontroller/save" id="myform" enctype="multipart/form-data" method="post"> first name:<br /> <input type="text" name="firstname" value="mickey"><br /> last name:<br /> <input type="text" name="lastname" value="mouse"><br /><br /> <button class="btn btn-success" type="submit">save</button> </form>
you can attach event handler directly submit
event of form
send ajax request using jquery's $.ajax()
method:
$("#myform").submit(function(e) { e.preventdefault(); // stop standard form submission $.ajax({ url: this.action, type: this.method, data: $(this).serialize(), success: function(data) { console.log(data); // object returned action displayed here. } }); });
Comments
Post a Comment