Introduction

This article shows how to use the RadioButton helper in MVC applications.

Create an ASP.Net Web Application as in Figure 1.

Figure 1: Web application

Choose the MVC template as in Figure 2.

Figure 2: MVC template

Add an Employee Controller as in Figure 3.

Figure 3: Add Controller

Figure 4: EmployeeController

EmployeeController.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace RadioButtonListApp_MVC.Controllers
  7. {
  8. public class EmployeeController : Controller
  9. {
  10. //
  11. // GET: /Employee/
  12. public ActionResult Index()
  13. {
  14. return View();
  15. }
  16. [HttpPost]
  17. public string Index(string gender)
  18. {
  19. string selectedGender = gender;
  20. return "Selected Gender is: " + selectedGender;
  21. }
  22. }
  23. }

Add a View as in Figures 5 and 6.

Figure 5: Add View

Figure 6: Index View

Index.cshtml

  1. @{
  2. ViewBag.Title = "Index";
  3. }
  4. <h2>Index</h2>
  5. @using (Html.BeginForm("Index", "Employee", FormMethod.Post))
  6. {
  7. @Html.RadioButton("gender", "Male")<text>Male</text>
  8. @Html.RadioButton("gender", "Female", true)<text>Female</text>
  9. <br />
  10. <input type="submit" value="Submit" />
  11. }
The output of the application is as in Figures 7 and 8.

Figure 7: Index

Figure 8: Selected value

Summary

In this article we have seen how to use the RadioButton helper in MVC applications.
Happy coding!