我在控制器里有这段代码:
[HttpPost]
public ActionResult Create(Student student)
{ //somecode..
我想要从这里提交:
<form method="post" action="/student/create">
<!-- the from contents-->
如何使用Ajax调用提交表单 我需要JQuery Ajax调用,以便提交这个表单。
同时,我想确认数据类型, 谢谢。
我在控制器里有这段代码:
[HttpPost]
public ActionResult Create(Student student)
{ //somecode..
我想要从这里提交:
<form method="post" action="/student/create">
<!-- the from contents-->
如何使用Ajax调用提交表单 我需要JQuery Ajax调用,以便提交这个表单。
同时,我想确认数据类型, 谢谢。
试试这个
var form = $('#formId');
$.ajax({
cache: false,
async: true,
type: "POST",
url: form.attr('action'),
data: form.serialize(),
success: function (data) {
alert(data);
}
});
如果您使用的是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>
}
那么它看起来应该像这样(没有查看您的视图绑定):
// 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
});