似乎我通过邮递员传递的数据与控制器方法中的模型不匹配。 我在.Net应用程序中实现Web api端点,以同时处理保存多个文件上传。用户可以选择多个文件,并且可以为每个文件选择文档类型和日期。记录插入到两个表(父表和子表)中 在我的示例中,父表是DOCUMENT表,子记录存储在MANAGERSTRATEGY_DOCUMENT表中。我的JSON结构有什么问题
Viewmodel
public class DocumentUploadCreateViewModel
{
public int ManagerStrategyId { get; set; }
public List<DocumentUploadCreateDetailsViewModel> DocumentUploadCreateDetailsViewModel = new List<DocumentUploadCreateDetailsViewModel>();
}
public class DocumentUploadCreateDetailsViewModel
{
public string Name { get; set; }
public int DocumentTypeId { get; set; }
public DateTime? DocumentDate { get; set; }
public byte[] DocumentContent { get; set; }
}
控制器代码
[HttpPost]
[SkipTokenAuthorization]
[Route("api/documentupload/create")]
public IHttpActionResult Create(DocumentUploadCreateViewModel model)
{
var mgrStrategyDocument = new MANAGERSTRATEGY_DOCUMENT();
var mgrDocumentService = GetService<DOCUMENT>();
var mgrStrategyDocumentService = GetService<MANAGERSTRATEGY_DOCUMENT>();
if (model != null)
{
foreach (var obj in model.DocumentUploadCreateDetailsViewModel)
{
var mgrDocument = new DOCUMENT
{
DOCUMENT_TYPE_ID = obj.DocumentTypeId,
DOCUMENT_DATE = obj.DocumentDate,
NAME = obj.Name
};
mgrDocument = mgrDocumentService.Create(mgrDocument);
var mangerStrategyDocumentService = GetService<MANAGERSTRATEGY_DOCUMENT>();
var mgrStrategyDocument1 = new MANAGERSTRATEGY_DOCUMENT()
{
DOCUMENT_ID = mgrDocument.ID,
MANAGERSTRATEGY_ID = model.ManagerStrategyId
};
mgrStrategyDocument = mgrStrategyDocumentService.Create(mgrStrategyDocument1);
}
}
return Ok(mgrStrategyDocument);
}
}
邮递员
http://localhost:56888/api/documentupload/create
JSON
{
"ManagerStrategyId" : 5508,
[
"Name" : "x2",
"DocumentTypeId" : 2,
"DocumentDate" : "15/04/2010",
"DocumentContent" : [71,107,98]
],
[
"Name" : "x3",
"DocumentTypeId" : 3,
"DocumentDate" : "15/04/2010",
"DocumentContent" : [71,107,98]
],
[
"Name" : "x4",
"DocumentTypeId" : 4,
"DocumentDate" : "15/04/2010",
"DocumentContent" : [71,107,98]
]
}
答案 0 :(得分:0)
您的JSON缺少列表的键,并且列表语法似乎不正确。请尝试以下操作:
{
"ManagerStrategyId":5508,
"DocumentUploadCreateDetailsViewModel":[
{
"Name":"x2",
"DocumentTypeId":2,
"DocumentDate":"2010-04-15",
"DocumentContent":[
71,
107,
98
]
},
{
"Name":"x3",
"DocumentTypeId":3,
"DocumentDate":"2010-04-15",
"DocumentContent":[
71,
107,
98
]
},
{
"Name":"x4",
"DocumentTypeId":4,
"DocumentDate":"2010-04-15",
"DocumentContent":[
71,
107,
98
]
}
]
}