How To Dynamic Upload multiple files with extra detail of model in asp.net core
1). firstly create Model File
namespace MyModel
{
public class InsertModel
{
public string Name { get; set; }
public List<uploadmodel> uploadmodels { get; set; }
}
public class uploadmodel
{
public int Id { get; set; }
public string Name { get; set; }
public IFormFile File { get; set; }
}
}
2).Create Action method of Get and Post Request
public IActionResult UploadModel()
{
InsertModel insertModel = new InsertModel();
insertModel.uploadmodels = new List<uploadmodel>();
for (int i = 0; i < 3; i++)
{
insertModel.uploadmodels.Add(new uploadmodel());
}
return View(insertModel);
}
[HttpPost]
public IActionResult UploadModel(InsertModel insertModel)
{
return View();
}
3).Create View Of Action
@model MyModel.InsertModel
@{
ViewData["Title"] = "UploadModel";
}
<h1>UploadModel</h1>
<h4>InsertModel</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="UploadModel" enctype="multipart/form-data" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
@{
if (@Model.uploadmodels != null)
{
for (int i = 0; i < Model.uploadmodels.Count; i++)
{
@Html.HiddenFor(m => m.uploadmodels[i].Id, new { id = "hidden"+i })
@Html.TextBoxFor(m=> m.uploadmodels[i].Name, new { id = "hidden"+i })
@Html.TextBoxFor(m=> m.uploadmodels[i].File, new { id = "hidden"+i ,@type="File"})
}
}
}
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>