简体   繁体   English

ASP.NET 核心嵌套 IFormFile 列表始终为 null

[英]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>
}        

result:结果: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 IFormFile 通过 JS 将文件上传到 ASP.Net Core - Upload Files via JS to ASP.Net Core with IFormFile ASP.NET Core API 中的模型绑定始终为空 - Model binding in ASP.NET Core API are always null ASP.net 核心 Razor 使用 AJAX 上传页面文件到 IFormFile 处理程序返回 404 Not Found - ASP.net core Razor page file upload with AJAX Post to IFormFile handler returns 404 Not Found 试图将包含 IFormFile 属性的 model 从 angular 传递到 asp.net 核心 web api - Trying to pass a model containing an IFormFile property from angular to asp.net core web api 在 ASP.Net Core 5 MVC 控制器中,当传递包含小数的 JSON 对象 FromBody 时,模型始终为空 - In ASP.Net Core 5 MVC Controller, when passed a JSON object FromBody that contains a decimal the model is always null ASP.NET MVC - 无法将 IFormFile 绑定到 Blob(图像) - ASP.NET MVC - Unable to bind IFormFile to Blob (Image) ASP.NET Core 中的 MVC 控制器的 AJAX POST 数据在传递超过一定长度的对象数组时始终为空 - AJAX POST Data to MVC Controller in ASP.NET Core Always Null When Passing An Object Array Over a Certain Length 将文件上传到 ASP.NET 内核。 数组始终为空 - Upload Files to ASP.NET Core. Array is always empty jQuery数据表-ASP.NET控制器参数始终为null - jQuery datatable - ASP.NET controller parameter always null asp.net mvc中的对象数组数据始终为null - objects array data in asp.net mvc is always null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM