How To Dynamic Upload multiple files with extra detail of model in asp.net core

 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>


3 comments:

  1. Hi, I am looking for a affordable web packages agency for my white label clients, If anyone know please let me know

    ReplyDelete
  2. If you're looking for a reliable web hosting provider, look no further than InMotion! Ranked in the top 10 U.S. providers and among the world's most renowned companies, this InMotion hosting review will give you an inside look into why InMotion is worth your consideration.

    ReplyDelete

Thank You For Your Great Contribution

Featured Post

how to find n number of nodes have child or cildren

 how to find n number of nodes have child or cildren for that we use recursive function  const   data = {     'id' : '0' ,...

Popular Posts