Asp.net MVC 4 send using ajax call

I have this in the controller:

[HttpPost] public ActionResult Create(Student student) { //somecode.. 

and I want to pass this:

 <form method="post" action="/student/create"> <!-- the from contents--> 

how to send this using an Ajax call I need a jQuery ajax call that submits this form.

and I want to make sure the data type, thanks

+6
source share
3 answers

try it

 var form = $('#formId'); $.ajax({ cache: false, async: true, type: "POST", url: form.attr('action'), data: form.serialize(), success: function (data) { alert(data); } }); 
+17
source

Use this if you are using a razor :

 @using (Ajax.BeginForm(new AjaxOptions(){ HttpMethod = "POST", Url = "your controller", OnComplete = "some client event" }) { <fieldset> <legend>This is a demo form.</legend> @Html.LabelFor(model => model.Name) @Html.TextBoxFor(model => model.Name) <input type="submit" value="Save" /> </fieldset> } 
+5
source

Well, that would look something (despite your view bindings):

 // serialize your form into JSON - maybe you have a different method of doing it var serializedForm = $("#YourFormId").serialize(); // post it to the server $.post("/student/create", serializedForm) .done(function (response) { // it done }) .fail(function (xhr, status, error) { // something bad happened }); 
+1
source

Source: https://habr.com/ru/post/957888/


All Articles