ASP.NET Core嵌套的IFormFile列表始终为null

编程入门 行业动态 更新时间:2024-10-27 22:30:09
本文介绍了ASP.NET Core嵌套的IFormFile列表始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用jquery AJAX发布具有List<IFormFile>的嵌套视图模型,但是IFormFile属性在模型上始终为空.

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.

这是我的嵌套模型(IFormFile在ChildModel内部):

Here is my nested Model (the IFormFile is inside the ChildModel):

public class ParentViewModel { public string ParentName{ get; set; } public ChildModel Child{ get; set; } }

ChildModel类:

ChildModel class:

public class ChildModel { public string ChildName{ get; set; } public IList<IFormFile> Images{ get; set; } }

控制器方法:

[HttpPost] public async Task<bool> CompleteAppointment(ParentViewModel viewModel) { // save logic return true; }

视图(这是局部视图.此处没有表格,需要在ajax调用中传递文档):

View (this is a partial view. No form here, need to pass documents on ajax call):

@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代码:

JavaScript Code:

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始终为null.我尝试过formData.append("Child[Images][i]", files[i]);,将IFormFile添加到包装器类中,然后在子级中使用它,以及其他一些选项.但没有一个.

viewModel.ChildModel.Images is always null. I have tried formData.append("Child[Images][i]", files[i]);, Adding IFormFile to a wrapper class then use it in the child, and few other options. But none works.

但是,有线连接是,如果我将public IList<IFormFile> Images{ get; set; }添加到ParentViewModel并附加为formData.append("Images", files[i]);,则文件将在控制器中可用.

However, the wired thing is if I add public IList<IFormFile> Images{ get; set; } to ParentViewModel and append as formData.append("Images", files[i]); , files become available in controller.

我在这里想念什么?我真的很感谢您的帮助.

What am I missing here? I really appreciate any help.

推荐答案

以下是一个演示示例:

控制器:

public IActionResult ModalPartial() { return View(); } public IActionResult Save(ParentViewModel pdata) { return View(); }

ModalPartial.cshtml:

ModalPartial.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> }

结果:

更多推荐

ASP.NET Core嵌套的IFormFile列表始终为null

本文发布于:2023-11-14 16:29:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587956.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   列表   NET   ASP   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!