I have a view which return a model in list .My view is as follows
Python
File Name
Download
@foreach (handbook.Data.EmpDoc files in Model)
{
@if (files.Filevalues == "0")
{
@files.Filename
@Html.ActionLink("Download", "DownloadFile", new { fileName = files.Filename, id = files.EmployeeId })
}
else
{
@files.Filename
@Html.ActionLink("Download", "DownloadFile", new { fileName = files.Filename, id = files.EmployeeId })
}
}I this view i need to add a text box below the table and save and retrieve its comments
but since the controller return list and to retrieve the files i a using foreach loop the text box is repeating for 5 times instead of 1.
If i add the text box alone in a separate div the asp-for is not recognized since i cant passit in for each loop.
my controller is as follows
C#
public async Task DocumentValidate(int? id)
{
if (id == null || _context.EmpDocs == null)
{
return NotFound();
}
var getinfo = await _context.EmployeeBasicInformations.FindAsync(id);
ViewBag.empid = getinfo.EmployeeId;
var foldername = getinfo.EmployeeEmail;
var docinfo = await _context.EmpDocs.FindAsync(id);
ViewBag.remarks = docinfo.HrRemarks;
EmpDoc lol = new EmpDoc();
string dirPath = Configuration["FolderPath:Document_Path"];
string path = Path.Combine(dirPath, foldername);
string[] folderpath = Directory.GetFiles(path);
EmpDoc docsss = new EmpDoc();
List files = new List();
foreach (string folderpaths in folderpath)
{
files.Add(new EmpDoc
{
Filename = Path.GetFileName(folderpaths),
EmployeeId = getinfo.EmployeeId,
Filevalues = Path.GetFileNameWithoutExtension(folderpaths)
});
}
return View(files);
}any model is as follows
C#
public partial class EmpDoc
{
[Key]
public int EmployeeId { get; set; }
public string? HrRemarks { get; set; }
[NotMapped]
public IFormFile File { get; set; }
[NotMapped]
public string Filename { get; set; }
[NotMapped]
public string Filevalues { get; set; }
}How to save and retrieve it. Please advice

Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 17, 2023, 11:29 PM EST
Hello Kaushik,
You can use below solution.
You can ceate a seprate viewmodel for you view. Example :
public class DocumentViewModel { public List Files { get; set; } public string HrRemarks { get; set; } }Then in your controller you can use it like below:\
//below foreach loop var viewModel = new DocumentViewModel { Files = files, HrRemarks = remarks }; return View(viewModel);And finally in your view
Thanks
Naimish Makwana