[英]ASP.NET Core nested IFormFile list is always null
I'm trying to post a nested viewmodel which has a List<IFormFile>
using a jquery AJAX post, however the IFormFile property is always null on the model. I'm trying to post a nested viewmodel which has a
List<IFormFile>
using a jquery AJAX post, however the IFormFile property is always null on the model.
Here is my nested Model (the IFormFile
is inside the ChildModel):这是我的嵌套 Model(
IFormFile
在 ChildModel 内):
public class ParentViewModel
{
public string ParentName{ get; set; }
public ChildModel Child{ get; set; }
}
ChildModel class:子型号 class:
public class ChildModel
{
public string ChildName{ get; set; }
public IList<IFormFile> Images{ get; set; }
}
Controller Method: Controller 方法:
[HttpPost]
public async Task<bool> CompleteAppointment(ParentViewModel viewModel)
{
// save logic
return true;
}
View (this is a partial view. No form here, need to pass documents on ajax call):查看(这是部分视图。这里没有表格,需要通过ajax调用文件):
@model SomeOtherViewModel
<div class="row">
//controls using SomeOtherViewModel
</div>
<div class="row">
<div class="col-md-6">
<div>
<span class="font-weight-bold">Child Name</span><br />
<div class="pt-2 pb-2"><input id="name" type="text" class="form-control" /></div>
<input type="file" id="images" multiple />
</div>
</div>
</div>
JavaScript Code: JavaScript 代码:
function save(){
var formData = new FormData();
formData.append("ParentName", "Anne");
formData.append("Child[ChildName]", "Sam");
var files = $("#images").get(0).files;
for (var i= 0; i< files.length; i++) {
formData.append("Child[Images]", files[i]);
}
$.ajax({
url: "FamilyController/Save",
type: "POST",
data: formData,
processData: false,
contentType: false,
success: function (data) {}
);
}
viewModel.ChildModel.Images
is always null. viewModel.ChildModel.Images
始终为 null。 I have tried formData.append("Child[Images][i]", files[i]);
我试过
formData.append("Child[Images][i]", files[i]);
, Adding IFormFile
to a wrapper class then use it in the child, and few other options. ,将
IFormFile
添加到包装器 class 然后在子级中使用它,以及其他几个选项。 But none works.但没有一个有效。
However, the wired thing is if I add public IList<IFormFile> Images{ get; set; }
但是,有线的事情是如果我添加
public IList<IFormFile> Images{ get; set; }
public IList<IFormFile> Images{ get; set; }
public IList<IFormFile> Images{ get; set; }
to ParentViewModel
and append as formData.append("Images", files[i]);
public IList<IFormFile> Images{ get; set; }
到ParentViewModel
和 append 作为formData.append("Images", files[i]);
, files become available in controller. ,文件在 controller 中可用。
What am I missing here?我在这里想念什么? I really appreciate any help.
我真的很感激任何帮助。
Here is a demo worked:这是一个演示:
Controller: Controller:
public IActionResult ModalPartial() {
return View();
}
public IActionResult Save(ParentViewModel pdata) {
return View();
}
ModalPartial.cshtml:模态部分.cshtml:
<div class="row">
<div class="col-md-6">
<div>
<span class="font-weight-bold">Child Name</span><br />
<div class="pt-2 pb-2"><input id="name" type="text" class="form-control" /></div>
<input type="file" id="images" multiple />
</div>
<button onclick="save()">save</button>
</div>
</div>
@section scripts{
<script type="text/javascript">
function save(){
var pdata = new FormData();
pdata.append('ParentName', 'Anne');
pdata.append('Child.ChildName', 'Sam');
var files = $("#images").get(0).files;
for (var i = 0; i < files.length; i++) {
pdata.append('Child.Images', files[i]);
}
$.ajax({
url: "Save",
type: "POST",
data: pdata ,
processData: false,
contentType: false,
success: function (data) { }
});
}
</script>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.