Introduction
This article shows how to use a RadioButtonFor helper handling HttpPost in MVC applications.
Create an ASP.Net Web Application as in Figure 1.
Figure 1: Web Application
Choose MVC template as in Figure 2.
Figure 2: MVC template
Add an Employee Controller as in Figures 3, 4 and 5.
Figure 3: Add Controller
Figure 4: MVC controller - empty
Figure 5: EmployeeController
EmployeeController.cs
- using RadioButtonForApp_MVC.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace RadioButtonForApp_MVC.Controllers
- {
- public class EmployeeController : Controller
- {
- //
- // GET: /Employee/
- public ActionResult Index()
- {
- Employee emp = new Employee();
- return View(emp);
- }
- [HttpPost]
- public string Index(Employee emp)
- {
- if (string.IsNullOrEmpty(emp.SelectedDepartments))
- {
- return "You did not select any option";
- }
- else
- return "You selected department is" + emp.SelectedDepartments;
- }
- }
- }
Figure 6: Employee
Employee.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace RadioButtonForApp_MVC.Models
- {
- public class Employee
- {
- public string SelectedDepartments { get; set; }
- public List<Department> Departments
- {
- get
- {
- EmployeeEntities db = new EmployeeEntities();
- return db.Departments.ToList();
- }
- }
- }
- }
Figure 7: Add ADO.NET Entity Framework
Figure 8: Connection setting
Add the View as in Figures 9 and 10.
Figure 9: Add View
Figure 10: Index View
Index.cshtml
- @model RadioButtonForApp_MVC.Models.Employee
- @{
- ViewBag.Title = "Index";
- }
- <h2>Index</h2>
- @using (Html.BeginForm("Index", "Employee", FormMethod.Post))
- {
- foreach (var department in Model.Departments)
- {
- @Html.RadioButtonFor(p => p.SelectedDepartments, department.DepartmentName)@department.DepartmentName
- }
- <br />
- <input type="submit" value="Submit" />
- }
The output of the application is as in the following:
Figure 11: Index
Summary
In this article we saw how to use a RadioButtonFor helper handling HttpPost in MVC applications.
Happy coding!

Santhakumar MunuswamyPosted May 31, 2015, 11:23 PM
Thanks for nice one