.Net Core 3.0 AJAX POST 主体始终为空
.Net Core 3.0 AJAX POST Body Always Null
我最近将我的项目从 .Net Framework 4.7 转换为 .Net Core 3.0。我的 AJAX post 无法正常工作。
以下是 .Net Framework 4.7 中的工作原理:
查看:
@using (Ajax.BeginForm("Save", "Controller", new AjaxOptions() { HttpMethod = "Post", OnSuccess = "OnSaveSuccess", OnFailure = "OnFailure" }, new { Model }))
{
... Model Code Here
}
控制器:
[HttpPost]
public JsonResult Save(Contract contract)
以下是 .Net Core 3.0 中不起作用的部分:
查看:
<form method="post" action="/Controller/Save" data-ajax="true" data-ajax-method="post" data-ajax-sucess="OnSaveSuccess" data-ajax-failure="OnFailure">
控制器:
[HttpPost]
public JsonResult Save([FromBody] Contract contract)
Contract 对象在此请求中为 NULL。我做错了什么吗?
谢谢
您可以参考下面的例子,使用jQuery Unobtrusive AJAX.
进行ajax提交
<form method="post" action="/Home/Save" data-ajax="true" data-ajax-method="post" data-ajax-success="OnSaveSuccess" data-ajax-failure="OnFailure">
<div class="form-group row">
<label asp-for="Name" class="col-sm-3 col-form-label"></label>
<div class="col-sm-7">
<input asp-for="Name" class="form-control">
</div>
</div>
<div class="form-group row">
<label asp-for="Email" class="col-sm-3 col-form-label"></label>
<div class="col-sm-7">
<input asp-for="Email" class="form-control">
</div>
</div>
<div class="form-group row">
<div class="col-sm-7 offset-sm-3">
<button class="btn btn-primary" id="submit">Submit</button>
</div>
</div>
</form>
获取并参考jquery-ajax-不显眼
@section scripts{
<script src="~/lib/jquery-ajax-unobtrusive/dist/jquery.unobtrusive-ajax.js"></script>
<script>
function OnSaveSuccess(res) {
console.log(res);
alert('Success');
}
function OnFailure() {
alert('failed');
}
</script>
}
合同class
public class Contract
{
public string Name { get; set; }
public string Email { get; set; }
}
控制器操作(具有 [FromForm] 属性)
[HttpPost]
public JsonResult Save([FromForm] Contract contract)
{
// code logic here
}
测试结果
结果是我的错。我放了:
$.ajaxSetup({
contentType: "application/json"
});
这导致 POST 无法正确映射。一旦我删除它,模型就正确地过来了。
我最近将我的项目从 .Net Framework 4.7 转换为 .Net Core 3.0。我的 AJAX post 无法正常工作。
以下是 .Net Framework 4.7 中的工作原理:
查看:
@using (Ajax.BeginForm("Save", "Controller", new AjaxOptions() { HttpMethod = "Post", OnSuccess = "OnSaveSuccess", OnFailure = "OnFailure" }, new { Model }))
{
... Model Code Here
}
控制器:
[HttpPost]
public JsonResult Save(Contract contract)
以下是 .Net Core 3.0 中不起作用的部分:
查看:
<form method="post" action="/Controller/Save" data-ajax="true" data-ajax-method="post" data-ajax-sucess="OnSaveSuccess" data-ajax-failure="OnFailure">
控制器:
[HttpPost]
public JsonResult Save([FromBody] Contract contract)
Contract 对象在此请求中为 NULL。我做错了什么吗?
谢谢
您可以参考下面的例子,使用jQuery Unobtrusive AJAX.
进行ajax提交<form method="post" action="/Home/Save" data-ajax="true" data-ajax-method="post" data-ajax-success="OnSaveSuccess" data-ajax-failure="OnFailure">
<div class="form-group row">
<label asp-for="Name" class="col-sm-3 col-form-label"></label>
<div class="col-sm-7">
<input asp-for="Name" class="form-control">
</div>
</div>
<div class="form-group row">
<label asp-for="Email" class="col-sm-3 col-form-label"></label>
<div class="col-sm-7">
<input asp-for="Email" class="form-control">
</div>
</div>
<div class="form-group row">
<div class="col-sm-7 offset-sm-3">
<button class="btn btn-primary" id="submit">Submit</button>
</div>
</div>
</form>
获取并参考jquery-ajax-不显眼
@section scripts{
<script src="~/lib/jquery-ajax-unobtrusive/dist/jquery.unobtrusive-ajax.js"></script>
<script>
function OnSaveSuccess(res) {
console.log(res);
alert('Success');
}
function OnFailure() {
alert('failed');
}
</script>
}
合同class
public class Contract
{
public string Name { get; set; }
public string Email { get; set; }
}
控制器操作(具有 [FromForm] 属性)
[HttpPost]
public JsonResult Save([FromForm] Contract contract)
{
// code logic here
}
测试结果
结果是我的错。我放了:
$.ajaxSetup({
contentType: "application/json"
});
这导致 POST 无法正确映射。一旦我删除它,模型就正确地过来了。