I have a view which return a model in list .My view is as follows
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
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
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 MakwanaPosted Feb 18, 2023, 4:29 AM
Hello Kaushik,
You can use below solution.
You can ceate a seprate viewmodel for you view. Example :
Then in your controller you can use it like below:\
And finally in your view
Thanks
Naimish Makwana