Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts,
ii am new in MVC. I have a Checkbox list. wich is bind from database.
C#
@foreach (var item in ViewBag.OtherSearch)
                           {
                               var idd = "other" + @item.ID;
                               <div class="col-lg-6">
                                   <input type="checkbox" class="chk_other" id=@idd value=@item.ID title=@item.SearchCriteria />
                                   <label for="checkbox" style="font-weight:normal; font-family:Calibri; font-size:12px;">@item.SearchCriteria</label>
                               </div>
                           }

Now my problem is . How i find the cheked Id on my Controller. I want to make a query on the basis of Checked Values. How To find them in Controller.

Thanks.
Posted

1 solution

Provide name to your checkbox:
HTML
@foreach (var item in ViewBag.OtherSearch)
                           {
                               var idd = "other" + @item.ID;
                               <div class="col-lg-6">
                                   <input type="checkbox" class="chk_other" id=@idd value=@item.ID title=@item.SearchCriteria name="chkbox" />
                                   <label for="checkbox" style="font-weight:normal; font-family:Calibri; font-size:12px;">@item.SearchCriteria</label>
</div>

And your action method signature would be:
C#
public ActionResult Index(FormCollection collection)
{
     if(!string.IsNullOrEmpty(collection["chkbox"])
     {
        string checkResp=collection["chkbox"];
        bool isChecked = Convert.ToBoolean(checkResp);
     }

}


-KR
 
Share this answer
 

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