Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a view which shows the list of files from a folder and the file count may differ for every user

my view is as follows


C#
<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)
                {
                    <tr style="text-align:center;">
                        <td>

                            <input type="checkbox" id="chk(@files.Filename)" />

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



and this is the script i am using to check if the check box is checked


C#
<script>
        // this is the scrpt for checkbox
        $(document).on("click", "[type='checkbox']", function (e) {
            if (this.checked) {
                $(this).attr("value", "1");
            } else {
                $(this).attr("value", "0");
            }
        });
    </script>


I am able to generate id for each file like chkDocs or chkpayslip but when i click in it, it sends value =0 to the controller

please help

What I have tried:
Posted
Updated 17-Feb-23 11:34am
Comments
Sandeep Mewara 13-Feb-23 10:32am    
You dont need a separate code to check the checkbox. Thats the default behaviour and believe whenever you are checking it, because of the code above would trigger post checked event and thus value will toggle to 0 always.

Remove the above code and on page submit, loop through checkboxes and see if the value is found as expected or not. It should.
Graeme_Grant 14-Feb-23 4:16am    
Which .Net Framework are you using?

Essentially, you need to drop the script and wrap the checkboxes in a form element for the data to post to the server. This is the requirement for any web server language, not just .Net.
Richard Deeming 15-Feb-23 4:32am    
As I told you previously, there is absolutely no point in changing the value of a checkbox when its checked state changes. Only the names and values of the checked checkboxes will be submitted to the server.

But if you want the value of the checked checkboxes to be sent to the controller, you need to give them a name. The code in your question only sets the id, so none of them will be sent.

1 solution

You need to just take care of setting the name of the checkboxes properly and you should be able to read the values in the post.

You can try something like below and it should have values posted in the controller action

Razor
<tbody>
                @for(int i=0; i < handbook.Data.EmpDoc.Count; i++)
                {
                    <tr style="text-align:center;">
                        <td>

                            <input type="checkbox" name="chk(@handbook.Data.EmpDoc[i].Filename)" />
</td>

                     
                        <td>@handbook.Data.EmpDoc[i].Filename</td>
                        <td class="text-nowrap">@Html.ActionLink("Download", "DownloadFile", new { fileName = handbook.Data.EmpDoc[i].Filename, id = handbook.Data.EmpDoc[i].EmployeeId })</td>
                    </tr>
                    
                }

            </tbody>
 
Share this answer
 
v3

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