【问题标题】:Passing List of Model Class From A View to A Controller in Asp.NetCore在 Asp.Net Core 中将模型类列表从视图传递到控制器
【发布时间】:2020-02-05 15:38:52
【问题描述】:

我目前正在尝试将 ModelClass(FileInfo) 列表发布到我的控制器,但提交似乎根本没有调用控制器,即使在我调试时,该过程也不会进入我的控制器操作,如下是我的查看代码(claimdocumentform.cshtml),

@model IList<PIBSSBus.DomainModels.FileInfo>


 <form method="POST" enctype="multipart/form-data">

          <table class="table table-striped table-hover table-bordered" id="sample_editable_1">

                                       <thead>

                                            <tr>

                                                <th> Document</th>

                                                <th> Submitted </th>

                                                <th> Date Submitted </th>

                                                <th> # </th>
                                            </tr>
                                        </thead>
                                        <tbody>

                                            <tr>
                                                <td> <input type="text" placeholder="title" asp-for="@Model[0].Title" value="enter">  </td>
                                                <td><input type="text" placeholder="Description" asp-for="@Model[0].Description" value="enter1"> </td>
                                                <td>  </td>

                                                <td>
                                                    <input type="file" class="" asp-for="@Model[0].File"  value="Upload">
                                                </td>
                                            </tr> 
                                            <tr>
                                                <td> <input type="text" placeholder="title" asp-for="@Model[1].Title" value="enter2"> </td>
                                                <td><input type="text" placeholder="Description" asp-for="@Model[1].Description" value="enter3"> </td>
                                                <td>  </td>

                                                <td>
                                                    <input type="file" class="" asp-for="@Model[1].File" value="Upload">
                                                </td>
                                            </tr> 

                                           @* @Html.AntiForgeryToken();*@
                                        </tbody>
                                    </table>'
<button type="submit" asp-controller="Claims" asp-action="Wazobia" class="btn green button-submit">
    Submit
    <i class="fa fa-check"></i>
</button>
</form>

这是我的控制器

 public IActionResult claimdocumentform()
    {
        //PIBSSBus.DomainModels.FileInfo something = new PIBSSBus.DomainModels.FileInfo();
       // IList<PIBSSBus.DomainModels.FileInfo> filess =new List<PIBSSBus.DomainModels.FileInfo>();
        return View();
    }
  //  [ValidateAntiForgeryToken]
    [HttpPost]
    public IActionResult Wazobia(IList<PIBSSBus.DomainModels.FileInfo> fam)
    {

        return View();
    }

这是我的模型类

public class FileInfo
{

    public string Title { get; set; }


    public string Description { get; set; }


    [DataType(DataType.Upload)]
    public IFormFile File { get; set; }
}

我试图将包含两行的 FileInfo 类列表传递给我的控制器操作 Wazobia 但它根本不调用该操作的问题,请帮助

【问题讨论】:

  • 我尝试了相同的代码,它运行良好。asp-controller 正确吗?你能不能在浏览器中按F12键检查你的开发工具的网络选项卡,然后查看请求是否发送。
  • @XingZou 1)。所以你的意思是,FileInfo 列表是在 fam 列表中的控制器操作 Wazobia 中收到的?因为我自己甚至根本没有进入控制器操作“Wazobia”,所以浏览器只会继续加载,直到它带来错误 2)。您是否像我在“claimdocumentform”控制器操作中那样返回了没有模型类的视图?
  • 是的...所以我建议你检查你的浏览器 F12 网络标签
  • 点赞i.stack.imgur.com/JS2KG.png看看你发了什么样的请求
  • @XingZou 这是错误imgur.com/a/FFZCj6q

标签: asp.net-core-2.2


【解决方案1】:

我首先在 asp.net core 3.0 中测试了代码,它运行良好(你也可以试试这个)。然后我尝试了asp.net core 2.2 MVC,它不像你的那样工作。

然后发现是asp.net core 2.2的bug,在asp.net core 3.0修复了,参考https://github.com/dotnet/core/issues/2523#issuecomment-481120637

一种解决方法是您在模型之外接收文件

public class FileInfoVM
{
    public string Title { get; set; }
    public string Description { get; set; }
}

public class FileInfo
{
    public string Title { get; set; }
    public string Description { get; set; }
    public IFormFile File { get; set; }
}

查看:

<tbody>

    <tr>
        <td> <input type="text" placeholder="title" asp-for="@Model[0].Title"  value="enter">  </td>
        <td><input type="text" placeholder="Description" asp-for="@Model[0].Description"  value="enter1"> </td>
        <td>  </td>

        <td>
            <input type="file" class=""  name="Files">
        </td>
    </tr>
    <tr>
        <td> <input type="text" placeholder="title" asp-for="@Model[1].Title"  value="enter2"> </td>
        <td><input type="text" placeholder="Description" asp-for="@Model[1].Description" value="enter3"> </td>
        <td>  </td>

        <td>
            <input type="file" class=""  name="Files">
        </td>
    </tr>


</tbody>

动作;

[ValidateAntiForgeryToken]
[HttpPost]
public IActionResult Wazobia(IList<PIBSSBus.DomainModels.FileInfoVM> fam,List<IFormFile> Files)
    {
        List<FileInfo> fileInfos = new List<FileInfo>();
        for(int i= 0; i< Files.Count; i++)
        {
            fileInfos.Add(new FileInfo()
            {
                Title = fam[i].Title,
                Description = fam[i].Description,
                File = Files[i]
            });
        }
        // save fileInfos 
        return View();
    }

【讨论】:

  • 非常感谢。好的,所以从 List 文件中检索后,我如何将文件与特定的标题和描述相匹配?因为我想将文件转换为 base64 字符串并保存每个文件及其标题和描述?
  • @Ajumobi Olamide 我编辑了回复。尝试使用新模型和操作实现更新我的代码。
猜你喜欢
  • 2015-10-06
  • 1970-01-01
  • 2019-09-03
  • 2021-02-24
  • 2013-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多