upsidedownwf
upsidedownwf

Reputation: 164

Passing List of Model Class From A View to A Controller in Asp.NetCore

i'm currently trying to post a a list of ModelClass(FileInfo) to my controller, but the submit doesn't seem to call the controller at all,even when i debug, the process does not enter my Controller Action, below is my View Code(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>

This is my controller

 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();
    }

this is my model class

public class FileInfo
{

    public string Title { get; set; }


    public string Description { get; set; }


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

The issue i am trying to pass the list of FileInfo class containing two rows to my controller action Wazobia but it doesn't call the action at all, pls help

Upvotes: 0

Views: 97

Answers (1)

Ryan
Ryan

Reputation: 20116

I firstly tested the code in asp.net core 3.0 and it works well(You could also try this). Then I tried in asp.net core 2.2 MVC and it does not work like yours.

Then I find that it is a bug in asp.net core 2.2 and is fixed in asp.net core 3.0,refer to https://github.com/dotnet/core/issues/2523#issuecomment-481120637

A workaround is that you receive the files outside of the model

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; }
}

View:

<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>

Action;

[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();
    }

Upvotes: 1

Related Questions