Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a html table in my view. I have checkboxes to select record and insert them into my table. here is my code :
<table id="dtsResult" class="table table-striped table-bordered" cellspacing="0" width="100%">
       <thead>
           <tr>
               <th>SelectAll<input type="checkbox" id="CheckAllRes" /></th>
               <th>Unit</th>
            </tr>
       </thead>
       <tbody>
           @foreach (var item in ViewBag.Result)
           {
               <tr>
                   <td><input type="checkbox" id=@item.RecordID name="res" class="chkres" /></td>
                   <td> @item.UnitNo</td>
               </tr>
           }
       </tbody>
   </table>

Now my problem is after insert record when i open again this page then i want to checked all check boxes which is already inserted. Can anyone help me with why and how to fix this so I checked these check boxes from db. Please help.
Thanks
Posted
Updated 11-May-18 2:54am
v2

HTML
@foreach (var item in ViewBag.Result)
          {
              <tr>
                  <td><input type="checkbox" id="@item.RecordID" name="res" class="chkres" checked="@item.IsChecked" /></td>
                  <td> @item.UnitNo</td>
              </tr>
          }


item.IsChecked has to be bool, and your ViewBag.Result must contain it.

-KR
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 14-Nov-15 3:57am    
Ah, similar ideas! :-)
Krunal Rohit 14-Nov-15 4:12am    
:-D

-KR
ErBhati 14-Nov-15 5:38am    
Thanks Krunal..
Krunal Rohit 14-Nov-15 6:10am    
Glad I could help.

-KR
<pre lang="c#">@foreach (var item in ViewBag.Result)
{
HTML
 <tr>
    <td>
         <input type="checkbox" @if(item.IsSaved){<text>checked</text>}
         id="@item.RecordID" name="res" class="chkres" /></td>
    <td>@item.UnitNo</td>
</tr>
C#
}
 
Share this answer
 
Comments
Richard Deeming 11-May-18 9:22am    
It's an old question, but as far as I can see, the accepted solutions don't work. If the checked attribute is present, the checkbox is checked. The attribute has to be omitted entirely - as you have done here - for the checkbox to be unchecked.

It might be simpler to use the Html.CheckBox helper method though. :)
The schema or task is unclear, as you have not explained how would you find the records that are added to the database. I would assume that your item object holds a property "IsSaved".

That said, the checkbox input can be altered for its state like this,

@foreach (var item in ViewBag.Result)
{
   <tr>
       <td>
            <input type="checkbox" checked="@item.IsSaved" 
            id="@item.RecordID" name="res" class="chkres" /></td>
       <td>@item.UnitNo</td>
   </tr>
}


This would now check for a value, either true or false. Then it would either check it, or leave it unchecked. That depends on your object. Implement it in a way that it represents your system.
 
Share this answer
 
v2

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