javascript - c# 从 Angular 发送 3 个参数到 c#mvc,我在服务器端得到 null

标签 javascript c# angular typescript post

我正在尝试传递 3 个参数(2 种类型和 1 个字符串), 但在服务器端参数为空

这是我的服务

const httpOptions = {
      headers: new HttpHeaders({
         'Content-Type': 'application/json '
      })
};
let body = {
  auditId: auditId,
  rolId: this.permisoService.currentUserRolValue.rolId,
  valores: JSON.stringify(valores),
}
return this.http.post<any>(this._saveURL, body, httpOptions).pipe(
  map(res => { return res; }),
  catchError(this.handleError)
);
}

和服务端

[HttpPost, Route("AuditMail/Save"), Produces("application/json")]
    public async Task<IActionResult> Save([FromBody] int auditId, int rolId , String valores)
    {
        return Json(await _repository.Save(auditId, rolId, valores));
    }

我尝试将内容类型更改为文本/纯文本,删除 [FromBody] 得到相同的结果,

我将不胜感激任何帮助,谢谢 POST

最佳答案

请参阅类似问题的答案:https://stackoverflow.com/a/49082613/12431728其中说:

You can not get primitive types from your body directly like [FromBody] string id if your route content type is application/json because mvc waits model to bind json body to model not primitive types.

我建议在您的 API 项目中创建一个新类来保存这些值:

public class AuditMailModel
{
    public int AuditId { get; set; }
    public int RolId { get; set; }
    public string Valores { get; set; }
}

然后在你的 Controller 中:

public async Task<IActionResult> Save([FromBody] AuditMailModel model)
{
    return Json(await _repository.Save(model.AuditId, model.RolId, model.Valores));
}

关于javascript - c# 从 Angular 发送 3 个参数到 c#mvc,我在服务器端得到 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59228519/

相关文章:

javascript - $(document).on ('click' ) 和 $ ('p[class="classname"]').on(' click') 之间的区别

javascript - 如何每秒将变量增加 (x) 个单位,然后将变量与 Date() 一起使用来确定自月初以来的单位数

c# - mysql随机记录给出一条重复数据

Angular 2 表格呈现缓慢

angular - Angular 2 中的导入是如何工作的?

javascript - 如何将ajax代码转换为php

javascript - babel cli 不会为浏览器生成 JS

c# - 如何在 EF Core 中连接多个表中仅包含某些列的数据集?

c# - 我如何使用 LAME 将 wav 编码为 mp3 c#

angular - 更改 ngForm 字段值