Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use CheckBoxList in Asp.Net MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 878
    Comment on it

    How to use CheckBoxList in Asp.Net MVC

    For using CheckBoxList in Asp.Net MVC, we have to follow the following steps:-

    Step1: First download the CheckBoxList from Nuget, Once it is downloaded we can see the reference of MVCCheckBoxList in the References folder of the project.

    Step 2: Now as the CheckBoxList is downloaded, We will create a model class for it.

    Example:

     public class Model
        {
           public IEnumerable<SelectListItem> chkbx { get; set; } 
           public string[] chkbxIds { get; set; }
        }
    

    IEnumerable chkbx will hold the value for checkboxes to be displayed, while string[] chkbxIds will hold the value of the selected checkboxes.

    Step 3: Now bind IEnumerable chkbx with values either by Database or with static values.

    Example: Binding CheckBoxList through Enum:

    //Creating Enum in the Model Class:
    
    public class Model
        {
           public IEnumerable<SelectListItem> chkbx { get; set; } 
           public string[] chkbxIds { get; set; }
    
             public enum CheckBoxOptions
              {
                DotNet = 1,
                Java = 2,
                Php = 3,
                JavaScript = 4
              }
        }
    
    //Creating instance of the Model class:
    
    Model model = new Model();
    
    IEnumerable<Model.CheckBoxOptions> checkBoxOptions = Enum.GetValues(typeof(Model.CheckBoxOptions))
                                                                                                                                 .Cast<Model.CheckBoxOptions>();
    
    model.chkbx = from action in checkBoxOptions
                                  select new SelectListItem {
                                   Text = action.ToString(),
                                   Value = ((int)action).ToString()
    
                                  };
    

    Now this will bind the value of Enum to the CheckBoxList.

    Step 4: Now write the following code in the View i.e. cshtml file:

    Add the following line at the top of the View Page:

    @using MvcCheckBoxList.Model
    

    Now add the following code, where you wan't to display the ckeckboxes:

    @Html.CheckBoxListFor(model => model.chkbxIds, model => model.chkbx, model => model.Value, model => model.Text, model => model.Selected, Position.Vertical)
    

    As discussed earlier model.chkbxIds will hold the values of the selected Checkboxes, model.chkbx will bind the CheckBoxList with model.Value and model.Text and Position.Vertical will display the Checkboxes in Vertical direction.

    Note:=> If we wan't to increase or decrease the number of checkboxes, we only have to make or delete an entry from the Enum CheckBoxOptions respectively.

    Hope it helps...!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: