将模型列表 Class 从视图传递到 Asp.NetCore 中的控制器
Passing List of Model Class From A View to A Controller in Asp.NetCore
我目前正在尝试 post 向我的控制器发送一个 ModelClass(FileInfo) 列表,但是提交似乎根本没有调用控制器,即使在我调试时,进程也没有输入我的Controller Action,下面是我的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>
这是我的控制器
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();
}
这是我的模型class
public class FileInfo
{
public string Title { get; set; }
public string Description { get; set; }
[DataType(DataType.Upload)]
public IFormFile File { get; set; }
}
问题是我试图将包含两行的 FileInfo class 列表传递给我的控制器操作 Wazobia,但它根本不调用操作,请帮助
我首先测试了asp.net core 3.0 中的代码,它运行良好(你也可以试试这个)。然后我在 asp.net 核心 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();
}
我目前正在尝试 post 向我的控制器发送一个 ModelClass(FileInfo) 列表,但是提交似乎根本没有调用控制器,即使在我调试时,进程也没有输入我的Controller Action,下面是我的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>
这是我的控制器
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();
}
这是我的模型class
public class FileInfo
{
public string Title { get; set; }
public string Description { get; set; }
[DataType(DataType.Upload)]
public IFormFile File { get; set; }
}
问题是我试图将包含两行的 FileInfo class 列表传递给我的控制器操作 Wazobia,但它根本不调用操作,请帮助
我首先测试了asp.net core 3.0 中的代码,它运行良好(你也可以试试这个)。然后我在 asp.net 核心 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();
}