javascript - 发送 JSON 对象。如何设置简单的 .net 控制器方法来接收这种类型?
问题描述
目前我正在从看起来完全像这样的角度发送对象。
{"department":2,"note":"asdf","weekEnding":"2019-11-02T00:00:00"}
然后我在我的 .net 控制器中使用这个方法,看起来像这样。
[HttpPost]
[Route("Edit")]
public void Edit([FromForm] CorpNotes newMessage)
{
//_SLGContext.Entry(newMessage).State = EntityState.Modified;
//_SLGContext.SaveChanges();
}
下面是我的 CorpNotes 模型
public class CorpNotes
{
public string Department { get; set; }
public string Note { get; set; }
public string WeekEnding { get; set; }
}
我在我的 Post 方法 Edit() 中收到变量 CorpNotes newMessage 中的所有空值
为什么我的所有值都为空? slg-corp-notes.service.ts
updateMessage(message: any) {
console.log("at service")
console.log(message)
var newMessage = new CorpNotes(message['departments'], message['noteBody'], message['weeks'].weekEnding)
var Params = '?Department=' + message['departments'] + '&Note=' + message['noteBody'] + '&WeekEnding=' + message['weeks'].weekEnding
console.log(newMessage)
console.log(JSON.stringify(newMessage))
console.log(Params)
const headers = new HttpHeaders()
.set('Content-Type', 'application/json;charset=UTF-8')
let options = { headers: headers };
return this.http.post(this.baseUrl + 'api/SlgCorpNotes/Edit', JSON.stringify(newMessage), options);
//return this.http.post(this.baseUrl + 'api/SlgCorpNotes/Edit', JSON.stringify(newMessage))
}
解决方案
JSON.stringify
从您的 Angularhttp.post
调用中删除。它应该只是能够识别一个对象。JSON 中的最后一个字段是日期,但 C# 模型需要一个字符串,也将其更改为日期。
推荐阅读
- powershell - 如何从数组中获取多个文件夹中的所有文件
- ios - 在 Swift 中按给定日期获取一天的开始
- python - 为什么 Arcgis 将我的地址放在海里?
- javascript - 从外部应用程序打开时的 WebView cookie 生命周期
- spring-boot - Spring Security ROLE_ADMIN 和 ROLE_USER 问题
- android - 如何在 Azure DevOps Pipelines 上指定 Android SDK 构建工具版本
- spring - Thymeleaf/SB 如何将变量从对象传递到链接元素的 href 属性?
- javascript - 具有身份验证的多个 zoid 组件 sdk
- node.js - 登录后如何转到特定的个人资料?
- ios - 如何在视图边框的随机位置上绘制圆圈