.Net Core 3.0 AJAX POST主体始终为空 [英] .Net Core 3.0 AJAX POST Body Always Null

查看:98
本文介绍了.Net Core 3.0 AJAX POST主体始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将我的项目从.Net Framework 4.7转换为.Net Core 3.0。我无法正常使用AJAX帖子。

I've recently converted my project from .Net Framework 4.7 to .Net Core 3.0. I'm having trouble getting my AJAX post to work.

.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。

The Contract object comes as NULL in this request. Is there something I'm doing wrong?

谢谢

推荐答案

最终是我的错。我已经输入了:

It ended up being my fault. I had put:

$.ajaxSetup({
   contentType: "application/json"
});

这导致POST无法正确映射。一旦删除它,模型就可以正确显示了。

This was causing the POST to not map properly. Once I removed it the Model came over correctly.

这篇关于.Net Core 3.0 AJAX POST主体始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆