Introduction: Radio buttons are used to allow users to select a single option from a set of mutually exclusive choices. In ASP.NET MVC, radio buttons can be easily created using HTML helper methods. This documentation provides a step-by-step guide on how to create radio buttons in an ASP.NET MVC application.
Step 1: Create a Model: First, create a model class to represent the data associated with the radio button. For example, let's create a "Gender" model with two options: Male and Female.
csharppublic class GenderModel
{
    public string SelectedGender { get; set; }
    public List<SelectListItem> GenderOptions { get; set; }
}
Step 2: Create a Controller: Next, create a controller to handle the rendering of the view and the processing of form submissions.
csharppublic class GenderController : Controller
{
    public IActionResult Index()
    {
        var model = new GenderModel
        {
            GenderOptions = new List<SelectListItem>
            {
                new SelectListItem { Text = "Male", Value = "Male" },
                new SelectListItem { Text = "Female", Value = "Female" }
            }
        };
        return View(model);
    }
    [HttpPost]
    public IActionResult Index(GenderModel model)
    {
        // Process the selected gender
        string selectedGender = model.SelectedGender;
        
        // Perform further actions or validations
        
        return RedirectToAction("Result");
    }
    public IActionResult Result()
    {
        // Display the result view
        return View();
    }
}
Step 3: Create a View: Now, create a view to display the radio buttons and handle the form submission.
html@model GenderModel
@using (Html.BeginForm())
{
    <div>
        <label>Select Gender:</label>
        <br />
        @foreach (var genderOption in Model.GenderOptions)
        {
            <label>
                @Html.RadioButtonFor(m => m.SelectedGender, genderOption.Value)
                @genderOption.Text
            </label>
            <br />
        }
    </div>
    <br />
    <input type="submit" value="Submit" />
}
Step 4: Configure Routing:
Make sure to configure the routing in RouteConfig.cs or Startup.cs to map the controller and action.
csharp// RouteConfig.cs
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Gender", action = "Index", id = UrlParameter.Optional }
);
Explanation:
- The model class GenderModelcontains two properties:SelectedGenderto hold the selected option andGenderOptionsto store the available options asSelectListItemobjects.
- In the controller's Indexaction, an instance of theGenderModelis created and passed to the view.
- The view uses the Html.RadioButtonForhelper method to generate radio buttons for each option. TheRadioButtonFormethod binds the selected value to theSelectedGenderproperty of the model.
- When the form is submitted, the HttpPostaction in the controller is triggered. The selected value can be accessed via theSelectedGenderproperty of the model.
- Additional processing or validation can be performed in the controller action as needed.
- Finally, the Resultaction can be used to display a result view or perform any further actions.
Conclusion: By following the steps outlined in this documentation, you can easily create radio buttons in ASP.NET MVC using HTML helpers. This allows users to select a single option from a set of choices and enables efficient data handling in your application.
 
 
 
0 Comments