【发布时间】:2020-11-10 22:22:21
【问题描述】:
我尝试了多种配置,但无法将数据传递给控制器。我错过了什么?
public class PartsImport
{
public string companyID { get; set; }
public string partsCategoryID { get; set; }
public string subCategoryID { get; set; }
public string partNumber { get; set; }
public string name { get; set; }
public string description { get; set; }
public string description2 { get; set; }
public string quantity { get; set; }
public string cost { get; set; }
public string price { get; set; }
public string vendorID { get; set; }
public string mfg { get; set; }
public string companyLocationID { get; set; }
}
控制器。此时partsImport为空。
[Authorize(Roles = "Admin")]
[HttpPost]
public async Task<IActionResult> ImportPartsData([FromBody] IEnumerable<PartsImport> partsImport)
{
...
}
这里是 ajax 请求以及一些示例 json 数据。尝试提交两个版本,但在到达控制器时继续为空。
var data = { "partsImport": [{ "companyID": "57", "partsCategoryID": "205", "subCategoryID": "", "partNumber": "1", "name": "PVC Conduit 1/2 inch by 10 Feet", "description": "1/2 inch", "description2": "", "quantity": "1", "cost": "2.40", "price": "3.10", "vendorID": "", "mfg": "", "companyLocationID": "156" }, { "companyID": "57", "partsCategoryID": "205", "subCategoryID": "", "partNumber": "2", "name": "PVC Conduit 3/4 inch by 10 Feet", "description": "3/4 inch", "description2": "", "quantity": "1", "cost": "3.03", "price": "3.91", "vendorID": "", "mfg": "", "companyLocationID": "156" }] };
//var data = [{ "companyID": "57", "partsCategoryID": "205", "subCategoryID": "", "partNumber": "1", "name": "PVC Conduit 1/2 inch by 10 Feet", "description": "1/2 inch", "description2": "", "quantity": "1", "cost": "2.40", "price": "3.10", "vendorID": "", "mfg": "", "companyLocationID": "156" }, { "companyID": "57", "partsCategoryID": "205", "subCategoryID": "", "partNumber": "2", "name": "PVC Conduit 3/4 inch by 10 Feet", "description": "3/4 inch", "description2": "", "quantity": "1", "cost": "3.03", "price": "3.91", "vendorID": "", "mfg": "", "companyLocationID": "156" }] ;
$.ajax({
url: "/purchasing/ImportPartsData",
type: "POST",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (response) {
},
error: function (xhr, errorType, exception) {
},
});
【问题讨论】:
-
我认为最简单的方法是删除
"partsImport":。
标签: ajax asp.net-core-mvc