Ticker

6/recent/ticker-posts

Creating CheckBox in ASP.NET MVC

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:

  1. 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" /> }
  1. In your corresponding Controller file (e.g., MyController.cs), add the following code snippet to handle the form submission:
csharp
public 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); } }
  1. Create a corresponding ViewModel class (e.g., MyViewModel.cs) to represent the data for the view:
csharp
public class MyViewModel { public bool IsSelected { get; set; } }

Explanation:

  1. In the View file, the Html.CheckBoxFor helper method is used to generate the checkbox input element based on the provided model property (m => m.IsSelected). The CheckBoxFor helper automatically binds the checkbox value to the corresponding property in the model.
  2. The Html.LabelFor helper 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 expression m => m.IsSelected.
  3. Inside the Controller file, the HttpGet action (MyAction) is responsible for rendering the initial view. It creates a new instance of the ViewModel (MyViewModel) and passes it to the view.
  4. The HttpPost action (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 using model.IsSelected. You can perform further processing or redirect to a success page.
  5. The ViewModel class (MyViewModel) defines a property IsSelected of type bool, 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.

Post a Comment

0 Comments