【发布时间】:2019-10-03 14:40:58
【问题描述】:
我有一个 ASP.Net MVC 4 应用程序,我将其移植到 ASP.Net Core 3.0 MVC。
我正在尝试移植此方法
[HttpPost]
public ActionResult DoSave(
[Bind(Prefix = "new")]IEnumerable<C_Data> newItems,
[Bind(Prefix = "updated")]IEnumerable<C_Data> updatedItems,
[Bind(Prefix = "deleted")]IEnumerable<C_Data> deletedItems))
{
}
在帖子 AJAX 中(在 Web 浏览器的 JavaScript 中)我将值作为 JSON 像这样发送
{
"new[0].Id":3,
"new[0].SID":"00000000-0000-0000-0000-000000000000",
"new[0].Name":"asd"
}
这是 C_Data 类
public class C_Data
{
[Key]
public int Id { get; set; }
public Guid SID { get; set; }
[Required]
[MaxLength(40)]
public string Name { get; set; }
}
但是这个动作执行的时候三个参数都是空的。
这是我在 ModelState
中遇到的错误“无法将 JSON 值转换为 C_Data”
谁能告诉我如何移植这个方法?
谢谢。
PD:此操作在 MVC 控制器而非 API 控制器中。
【问题讨论】:
-
你知道
BindAttribute是用于表单而不是json?你的 json 应该是{"newItems":[{"Id" : 3,"SID":...}], "updatedItems":[...]}
标签: c# asp.net-mvc asp.net-core