How to get CheckBox values back to VIEW

2019-09-05 13:17发布

问题:

I have a full list of codes and their descriptions, 24 records in a DB

I do a select from the DB using stored Proc, and output this to the VIEW().

The user clicks on these checkboxes, how many he wishes, and it gets posted to the DB into another User table.

I scan through this User Table to get the data that he has checked .. say out of 24 he checks 10 checkboxes.

I want to be able to output all the checkboxes with the relevant boxes that were checked by the user. I have the data, where ALL the checkboxes are in a list sitting in VIEWBAG. and i also have the User Table that contains the data that he checked also in a seprate List, in a VIEWBAG so that i can access this in the VIEW.

How do i go about outputting ALL the checkboxes but also checking the checkboxes that were clicked by the user using the VIEWBAG passed from the controller?

The below code, just loops through ALL the data and outputs ALL checkboxes. I need to make sure the ones in the new VIEWBAG.USER are used to check the relevant boxes below:

    @foreach (var item in ViewBag.Expertise)
    {
        l_count = l_count + 1;
        l_code = l_code + 1;

        if (l_count == 1)
        {                                                          
            <td class="tdcheckbox">
                <input type="checkbox" name="code@(l_code)" value="@item.expertiseCode" />
                <b>@item.expertiseDesc</b>
            </td> 
            else if (l_count < 3)
            {    
                <td class="tdcheckbox">
                    <input type="checkbox" name="code@(l_code)" value="@item.expertiseCode" />
                    <b>@item.expertiseDesc</b>
                </td>
            }
            else if (l_count == 3)
            {           
                <td class="tdcheckbox">
                    <input type="checkbox" name="code@(l_code)" value="@item.expertiseCode" />
                    <b>@item.expertiseDesc</b>
                </td>                                                            
            }
            else if (l_count % 2 == 0 && l_count == 4)
            {         
                <td class="tdcheckbox">
                    <input type="checkbox" name="code@(l_code)" value="@item.expertiseCode" />
                    <b>@item.expertiseDesc</b>

                @if (@item.expertiseCode == "OTHER")
                {
                    @Html.TextBox("@item.otherExpertise")
                    <br /><font color="red">Comma separate specific expertise</font>
                }
                </td>   

                @:<tr></tr>
                l_count = 0;

回答1:

If I understand you question properly you can do the following.

put this at the top of your view

@{var ids = (List<int>)ViewBag.USERS);

Then when your are looping through all the checkboxes you can do the following.

if (ids != null && ids.Contains(item.expertiseCode))
{
   <input type="checkbox" name="code@(l_code)" value="@item.expertiseCode" checked />
}
else
{
  <input type="checkbox" name="code@(l_code)" value="@item.expertiseCode" />
}