Javascript 为什么我的一些值没有被webapi填充
我正在开发一个web api控制器。我建立了一个DTO来保存一张便条Javascript 为什么我的一些值没有被webapi填充,javascript,c#,asp.net-web-api,Javascript,C#,Asp.net Web Api,我正在开发一个web api控制器。我建立了一个DTO来保存一张便条 public class NoteContainer { public long? NoteId { get; set; } public string Type { get; set; } public string NoteText { get; set; } public NoteContainer(Note note, string type = null) {
public class NoteContainer
{
public long? NoteId { get; set; }
public string Type { get; set; }
public string NoteText { get; set; }
public NoteContainer(Note note, string type = null)
{
NoteId = note.Id;
NoteText = note.NoteText;
Type = type;
}
}
我的控制器中有一个方法:
[HttpPost]
public void EditNote(NoteContainer container)
{
//do work here
}
在从客户端发送NoteContainer之前,它具有所有值。当它到达服务器时,type为null!我应该不使用名为type的变量吗?为什么我会失去价值
我使用Postman发送此json:
{
"noteId": 10,
"type": "person",
"noteText": "loves broccoli",
}
我相信这需要默认构造函数。问题可能是
Note
类首先被实例化,然后被赋予notecainer
显示您正在发送的JSON。我的猜测是它有小写的“type”而不是“type”。ModelBinder中出现了一些问题。您是否在表单组件上手动键入name
属性(例如
)尝试以下操作:{“NoteId”:10,“Type”:“person”,“NoteText”:“loves brocoli”}如果没有看到用于发送此请求的javascript,看起来您正在js中创建一个具有3个属性的对象,并将其发送到EditNote(…)
——它看不到类型,因为它不是作为单独的参数发送的(会note.type
工作吗?)。同样,猜测,但是发布你的客户端代码会有所帮助。js发送的值与邮递员发送的值完全相同。大写与添加默认构造函数无关,神奇的是,我所有的值都被设置好了。但我不认为这正是你所说的。我认为这与NoteContainer构造函数中的默认参数有关