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

查看:30
本文介绍了.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天全站免登陆