Creating CheckBox in ASP.NET MVC
Introduction: In ASP.NET MVC, you can use the CheckBox HTML helper to create checkboxes in your views. Checkboxes are commonly used for allowing users to select multiple options or toggle a specific state.
Syntax: The syntax for creating a CheckBox using the CheckBox HTML helper in ASP.NET MVC is as follows:
csharp@Html.CheckBox(string name, bool isChecked)
Parameters:
- name(string): The name of the checkbox control.
- isChecked(bool): Specifies whether the checkbox should be checked or not.
Example:
- In your View file (e.g., MyView.cshtml), add the following code snippet to create a checkbox:
csharp@model MyViewModel
@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post))
{
    <div class="form-group">
        @Html.CheckBoxFor(m => m.IsSelected)
        @Html.LabelFor(m => m.IsSelected, "Select Option")
    </div>
    
    <input type="submit" value="Submit" class="btn btn-primary" />
}
- In your corresponding Controller file (e.g., MyController.cs), add the following code snippet to handle the form submission:
csharppublic class MyController : Controller
{
    [HttpGet]
    public ActionResult MyAction()
    {
        // Populate the model and pass it to the view
        var model = new MyViewModel();
        return View(model);
    }
    [HttpPost]
    public ActionResult MyAction(MyViewModel model)
    {
        if (ModelState.IsValid)
        {
            // Handle the form submission logic
            // Access the selected checkbox value using model.IsSelected
            // Redirect to a success page or perform further actions
            return RedirectToAction("Success");
        }
        // If the form submission is invalid, return the view with validation errors
        return View(model);
    }
}
- Create a corresponding ViewModel class (e.g., MyViewModel.cs) to represent the data for the view:
csharppublic class MyViewModel
{
    public bool IsSelected { get; set; }
}
Explanation:
- In the View file, the Html.CheckBoxForhelper method is used to generate the checkbox input element based on the provided model property (m => m.IsSelected). TheCheckBoxForhelper automatically binds the checkbox value to the corresponding property in the model.
- The Html.LabelForhelper is used to display a label for the checkbox. The label's text is set to "Select Option" and is associated with the checkbox using the property expressionm => m.IsSelected.
- Inside the Controller file, the HttpGetaction (MyAction) is responsible for rendering the initial view. It creates a new instance of the ViewModel (MyViewModel) and passes it to the view.
- The HttpPostaction (MyAction) handles the form submission. It receives the ViewModel as a parameter, and if the model is valid, you can access the selected checkbox value usingmodel.IsSelected. You can perform further processing or redirect to a success page.
- The ViewModel class (MyViewModel) defines a propertyIsSelectedof typebool, which represents the value of the checkbox.
By following these steps, you can create a checkbox in ASP.NET MVC, handle its value in the controller, and perform actions based on the selected state.
 
 
 
0 Comments