jQuery asp.net MVC 4 从 ajax 调用提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19924079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:49:33  来源:igfitidea点击:

asp.net MVC 4 submit from with ajax call

jqueryajaxasp.net-mvc-4

提问by Muath

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.

如何使用 Ajax 调用提交这个我需要让这个表单提交的 JQuery ajax 调用。

and i want to make sure about datatype , thanks

我想确定数据类型,谢谢

回答by Mohit Kumar

try this

尝试这个

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

回答by Besher

Use this, assuming you are using razorviews:

使用这个,假设您使用的是剃刀视图:

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

回答by Dimitar Dimitrov

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
    });