6

I have this in the controller:

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

and i want to submit this from:

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

how to submit this from using Ajax call i need the JQuery ajax call that lets this form submitted.

and i want to make sure about datatype , thanks

3 Answers 3

18

try this

var form = $('#formId');
$.ajax({
  cache: false,
  async: true,
  type: "POST",
  url: form.attr('action'),
  data: form.serialize(),
  success: function (data) {
    alert(data);
 }
});
Sign up to request clarification or add additional context in comments.

3 Comments

form = document.forms[2]--- when i use form.serialize() , i have type error [serialize is not function] JQuery already included.. how to solve it?
can you please tell me that how can we trigger default mvc validation before submitting form?
@UmarAbbas you can try like this:- jsfiddle.net/cfwx3Ld5 and use razor form instread of html
5

Use this, assuming you are using razor views:

@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>
}

1 Comment

You're missing a end parens in your using. S/B: OnComplete = "some client event"}))
1

Well it would look something like (that's without looking at 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's done
    })
    .fail(function (xhr, status, error) { 
       // something bad happened
    });

Comments

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.