我正在尝试使用 FormData 将文件和列表发布到 MVC 控制器,但列表在点击控制器时似乎为空。
表格数据:
var formData = new FormData();
formData.append("AttachedFile", files[0]);
formData.append("Items", invoice.serialize());
formData.append("CustomerId", 1);
formData.append("RevenueHeadId", demandNoteObject.RevenueHeadId);
模型:
public int CustomerId { get; set; }
public int RevenueHeadId { get; set; }
[DataType(DataType.Upload)]
public HttpPostedFileBase AttachedFile { get; set; }
public List<Items> Items { get; set; }
控制器:
[HttpPost]
public ActionResult Create(InvoiceCreateModel model)
查询:
$.ajax({
type: "POST",
url: "@Url.Action("Create", "")",
datatype: "Json",
data: formData,
cache: false,
contentType: false,
processData: false,
success: function (data) {
$.ajax({
type: 'POST',
url: '@Url.Action("Create", "yourControllerName")',
data: formData,
contentType: false,
processData: false,
success: function (data) { ...}
});
试试这个
编辑 因为 Items 是对象的集合,所以你必须为每个对象添加一个索引
var index = 0;
for(var item of invoice){
var pair = item[key];
formData.append("Items[" + index + "].yourField", pair.yourField);
index++;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句