Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have a view which return a model in list .

My view is as follows
HTML
<div class="container" style="padding: 0; height: 100%; width: 100%; margin: 0">
                    
    @foreach (handbook.Data.EmpDoc files in Model)
    {
        @if (files.Filevalues == "0")
        {
        }
        else 
        {
        }
    }

    <table class="table table-bordered table-striped">
        <thead>
            <tr style="text-align: center">                                
                <th>File Name</th>                                
                <th>Download</th>
            </tr>
        </thead>
        <tbody>
            <tr style="text-align: center">                                        
                <td></td>
                <td>@files.Filename</td>                                        
                <td class="text-nowrap">@Html.ActionLink("Download",     "DownloadFile", new { fileName = files.Filename, id = files.EmployeeId })</td>
            </tr>
            <tr style="text-align: center">
                <td></td>
                <td>@files.Filename</td>
                <td class="text-nowrap">@Html.ActionLink("Download", "DownloadFile", new { fileName = files.Filename, id = files.EmployeeId })</td>
            </tr>
        </tbody>
    </table>
</div>


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.

What I have tried:

my controller is as follows
C#
public async Task<iactionresult> 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<empdoc> files = new List<empdoc>();

    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
Posted
Updated 19-Feb-23 23:32pm
v3

1 solution

Since the remarks are related to the employee rather than the document, you'll need to create a view-model containing the remarks and the list of documents.

For example:
C#
public class EmployeeInformation
{
    public int EmployeeId { get; set; }
    public string? HrRemarks { get; set; }
    public List<EmployeeDocument> Documents { get; set; }
}

public class EmployeeDocument
{
    public string Filename { get; set; }
    public string Filevalues { get; set; }
    public IFormFile File { get; set; }
}
C#
public async Task<IActionResult> DocumentValidate(int? id)
{
    if (id == null || _context.EmpDocs == null)
    {
        return NotFound();
    }

    var employeeInfo = await _context.EmployeeBasicInformations.FindAsync(id);
    if (employeeInfo == null)
    {
        return NotFound();
    }
    
    var docInfo = await _context.EmpDocs.FindAsync(id);
    if (docInfo == null)
    {
        return NotFound();
    }
    
    var model = new EmployeeInformation()
    {
        EmployeeId = employeeInfo.EmployeeId,
        HrRemarks = docInfo.HrRemarks,
    };

    string foldername = employeeInfo.EmployeeEmail;
    string dirPath = Configuration["FolderPath:Document_Path"];
    string path = Path.Combine(dirPath, foldername);

    string[] files = Directory.GetFiles(path);
    model.Documents = new List<EmployeeDocument>(files.Length);
    
    foreach (string filePath in files)
    {
        model.Documents.Add(new EmpDoc
        {
            Filename = Path.GetFileName(filePath),
            Filevalues = Path.GetFileNameWithoutExtension(filePath)
        });
    }

    return View(model);
}
Razor
@model EmployeeInformation

<div class="container" style="padding: 0; height: 100%; width: 100%; margin: 0">
                    
    <table class="table table-bordered table-striped">
        <thead>
            <tr style="text-align: center">                                
                <th>File Name</th>                                
                <th>Download</th>
            </tr>
        </thead>
        <tbody>
        @for (int i = 0; i < Model.Documents.Count; i++)
        {
            var file = Model.Documents[i];
            
            <tr style="text-align: center">                                        
                <td></td>
                <td>@files.Filename</td>                                        
                <td class="text-nowrap"><a asp-action="DownloadFile" asp-route-filename="@file.Filename" asp-route-id="@Model.EmployeeId">Download</a></td>
            </tr>
        }
        </tbody>
    </table>
    
    <div>
        Comments:
        @Model.HrComments
    </div>
</div>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900