0

I have got a checboxlist in edit products page, I want the checkbox list to be checked depending on the db values.

For instance I am saving the values in db as 1,2,3 Therfore the checkboxlist with the value 1 , value 2 , value 3 should be checked.

Below is the code: Controller:

 public ActionResult Edit(int id)
    {
        ITrackdayRepository trackdayResp = new TrackdayRepository();
        IQueryable<Object> getAllproducts = trackdayResp.GetProductsSelectlist();

        ViewData["products"] = new SelectList(getAllproducts.ToList(), "productID", "Name");//all events for checkboxlist
        IVoucherRepository voucherResp = new VoucherRepository();
        Voucher voucher = voucherResp.GetVoucher(id);
        return View(voucher);
    }

    //
    // POST: /Admin/Voucher/Edit/5

    [HttpPost]
    public ActionResult Edit(int id, FormCollection collection)
    {
        try
        {
            // TODO: update the selected checkbox nd do db insert

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

View:

 <tr>
        <td>
        <label>Products</label>
        </td>
        <td>
         <% foreach (var item in (SelectList)ViewData["products"]) { %>
                 <input type="checkbox" name="Name" value="<%=item.Value %>" />
                  <label for="<%=item.Value%>"><%=item.Text%></label>
                  <br />

        <% } %>  
        </td>
        </tr>

edited:

 <td>
         <% foreach (var item in (SelectList)ViewData["events"]) { %>


                     <%var test = ViewData["arrays"];  %>
                    <%string checkString = test.ToString().Contains(item.Value) ? "checked=\"checked\"":string.Empty; %>
                 <input type="checkbox" name="Name" value="<%=item.Value %>" checked="<%=checkString %>" />
                  <label for="<%=item.Value%>"><%=item.Text%></label>
                  <br />

        <% } %>  
        </td>

1 Answer 1

1

To get a checkbox to be checked you need to set the checked attribute on the input element

<input type="checkbox" name="Name" value="<%=item.Value %>" checked="checked" />

EDIT:

Assuming you always want 1, 2, or 3 checked you can do this:

  <% var valuesToCheck = new List<int>() { 1, 2, 3 };

    foreach (var item in (SelectList)ViewData["products"]) { 
       string checkedString = valuesToCheck.Contains(item.productID) ? "checked=/"checked/" : string.empty;
    %>
     <input type="checkbox" name="Name" value="<%=item.Value %>" <%=checkedString%> />
     <label for="<%=item.Value%>"><%=item.Text%></label>
     <br />

<% } %>  

I would probably pass over the values you want checked as part of your viewmodel or if you aren't using one in ViewData so as to not define a list in the view.

Sign up to request clarification or add additional context in comments.

10 Comments

I know that but i dont want to check all the checkboxes just the checkbox which has values matching db values
@Mr A - which object/property contains the db values and which contains all the other values?
item.productID contains values like 1,2,3 therfore I want the checked box to be checked whose value is 1 nd 2 nd 3
@Mr A - So you always want 1,2,3 to be checked? If item.productID can be other values, how do you know which values you want to check?
no that was just an example , the values can change item.productId can have 5,4,6 etc these are the ids which are saved in the db , What I ma thinking to separate these values from comma and save in the array and then somehow passed to checkbox to be checked , is this possible
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.