RadioButtonFor Helper Handle IsSelected in MVC

Introduction

This article shows you how to use a RadioButtonFor helper handling IsSelected 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 and 4.

 

Figure 3: Add Controller.

 

Figure 4: Employee Controller.

EmployeeController.cs

  1. using RadioButtonForApp_MVC.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7.   
  8. namespace RadioButtonForApp_MVC.Controllers  
  9. {  
  10.     public class EmployeeController : Controller  
  11.     {  
  12.         //  
  13.         // GET: /Employee/  
  14.         public ActionResult Index()  
  15.         {  
  16.             Employee emp = new Employee();  
  17.             return View(emp);  
  18.         }  
  19.   
  20.   
  21.         [HttpPost]  
  22.         public string Index(Employee emp)  
  23.         {  
  24.             if (string.IsNullOrEmpty(emp.SelectedDepartments))  
  25.             {  
  26.                 return "You did not select any option";  
  27.             }  
  28.             else  
  29.                 return "You selected department is" + emp.SelectedDepartments;  
  30.   
  31.         }  
  32.     }  
  33. }  

Create an Employee Class as in Figures 5 and 6.

 

Figure 5: Employee Class

 

Figure 6: Employee Class

Employee.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace RadioButtonForApp_MVC.Models  
  7. {  
  8.     public class Employee  
  9.     {  
  10.         public string SelectedDepartments { getset; }  
  11.         public List<Department> Departments  
  12.         {  
  13.             get  
  14.             {  
  15.                 EmployeeEntities db = new EmployeeEntities();  
  16.                 return db.Departments.ToList();  
  17.             }  
  18.         }  
  19.     }  
  20. }  

Set up the Entity Framework as in Figures 7 and 8.

 

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 

  1. @model  RadioButtonForApp_MVC.Models.Employee  
  2.   
  3. @{  
  4.     ViewBag.Title = "Index";  
  5. }  
  6.   
  7. <h2>Index</h2>  
  8. @using (Html.BeginForm("Index", "Employee", FormMethod.Post))  
  9. {  
  10.     foreach (var department in Model.Departments)  
  11.     {  
  12.   
  13.         @Html.RadioButtonFor(p => p.SelectedDepartments, department.DepartmentName, department.IsSelected == true ? new { Checked = "checked" } : null)@department.DepartmentName  
  14.     }  
  15.     <br />  
  16.     <input type="submit" value="Submit" />  
  17. }  

The output of the application is as in the following.

 

Figure 11: Index

 

Figure 12: IsSelected

Summary

In this article we saw how to use the RadioButtonFor helper handling IsSelected in MVC applications.
Happy coding!


Similar Articles
MVC Corporation
MVC Corporation is consulting and IT services based company.