0

I'm working with Dynamic Checkbox, I trying to get CheckBox Value from another already Created table. But When I Do Code For DropDownList it can Be Working but same as it is not working for CheckBox

  1. following is My Method

    public IEnumerable<clsHobbyList> GetHobby()
            {
                List<clsHobbyList> lstHobby = new List <clsHobbyList>();
    
                using (SqlConnection con = new SqlConnection(ConnectionString))
                {
                    SqlCommand cmd = new SqlCommand("spAddHoby", con);
                    cmd.CommandType = CommandType.StoredProcedure;
    
                    con.Open();
                    SqlDataReader rdr = cmd.ExecuteReader();
    
                    while (rdr.Read())
                    {
                        clsHobbyList hby = new clsHobbyList();
                        hby.Id = Convert.ToInt32(rdr["Id"]);
                        hby.HobbyName = rdr["HobbyName"].ToString();
    
    
                        lstHobby.Add(hby);
                    }
                    con.Close();
                }
                return lstHobby;
            }
    
  2. Following Is My Controller

     public ActionResult Create()
            {
                EmployeeDataAccessLayer objemployee = new EmployeeDataAccessLayer();
                clsHobbyList hby = new clsHobbyList();
                ViewBag.Hobby = new SelectList(objemployee.GetHobby(), "Id", "HobbyName");
    
                return View();
            }
    
  3. and this is my View Page, hear How Can I Get CheckBox ?? please suggest me some solution

    <div class="form-group">
                @Html.LabelFor(model => model.Hobby, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
    
                    @Html.DropDownList("Hobby", null, htmlAttributes: new { @class = "form-control" })
    
                </div>
                @Html.ValidationMessageFor(model => model.Hobby, "", new { @class = "text-danger" })
            </div>
    

    -----------Hear How Can I Get Checkbox--------------------------------------------

3

1 Answer 1

1

use Foreach loop

@foreach (var item in Model.clsHobbyList)
            {
                <input type="checkbox" id="@item.Id" value="@item.HobbyName" text="@item.HobbyName" name="@item.HobbyName" />
                @item.HobbyName
            }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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