mardi 14 février 2023

How to check and uncheck dynamically valued checkbox inside foreach loop in razor

I have a view which shows files from folder and the number of files may vary from user to user

I am dynamically creating checkbox and capturing its values based on filename for each row

the code is as follows

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

My controller is as follows this passes value to view

            var getinfo = await _context.EmployeeBasicInformations.FindAsync(id);
            ViewBag.empid = getinfo.EmployeeId;
            var foldername = getinfo.EmployeeEmail;
            string dirPath = Configuration["FolderPath:Document_Path"];
            string path = Path.Combine(dirPath, foldername);
            string[] folderpath = Directory.GetFiles(path);
        List<EmpDoc> files = new List<EmpDoc>();
            foreach (string folderpaths in folderpath)
            {
                files.Add(new EmpDoc
                {
                    Filename = Path.GetFileName(folderpaths),
                    EmployeeId = getinfo.EmployeeId,

                }) ;
            }
            return View(files);

MyModel is as follows

    public int EmployeeId { get; set; }
        public string? Form1{ get; set; }
        public string? Form2 { get; set; }
        public string? EnteredBy { get; set; }
        public DateTime? EnteredDatetime { get; set; }
        public string? UpdatedBy { get; set; }
        public DateTime? UpdatedDatetime { get; set; }
        public string? ValidatedBy { get; set; }
        public string? Form3 { get; set; }
        public string? Form4 { get; set; }
        public string? Form5 { get; set; }

        [NotMapped]
        public IFormFile File { get; set; }

        [NotMapped]
        public string Filename { get; set; }

the files are stored as form1.pdf, form2.pdf

when i check a checkbox for example if i am checking form1 checkbox , in db under form1 column i am saving it as 1 and 0 for non checked .

I am able to save the data, but i am facing issue when i relogin the page , i am unable to retrieve the values and pass it to view

my view is as follows

@foreach (handbook.Data.EmpDoc files in Model)
                            {
                                @if (files.Filevalues == "1")
                                {
                                    <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>
                                }


                            }



Aucun commentaire:

Enregistrer un commentaire