kaushik guru

kaushik guru

  • NA
  • 252
  • 30.3k

How to save and retrieve the values of textbox when model return list

Feb 18 2023 12:35 AM

I have a view which return a model in list .My view is as follows

<div class="container" style="padding-top:10px; height:100%; width:100%;padding:0;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>
            @foreach (handbook.Data.EmpDoc files in Model)
            {
                @if (files.Filevalues == "0")
                {
                    <tr style="text-align:center;">
                        <td>

                          <input type="checkbox" id="@files.Filevalues" name="@files.Filevalues" value="1" checked="checked" />

                        </td>
                        <td>@files.Filename</td>
                        <td class="text-nowrap">@Html.ActionLink("Download", "DownloadFile", new { fileName = files.Filename, id = files.EmployeeId })</td>
                    </tr>

                    
                }
                else 
                {
                    <tr style="text-align:center;">
                        <td>

                            <input type="checkbox" id="@files.Filevalues" name="@files.Filevalues" value="0" />

                        </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.

my controller is as follows

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

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


Answers (1)