Generating a RadioButtonList Control in MVC Using HTML Helpers

Introduction

This article shows how to use a RadioButtonList control in MVC using a HTML helper and binding with LINQ to SQL.

Database structure

Create a table in a database with the name department.

The following is the create table code for the department table:

  1. CREATE TABLE [dbo].[department]  
  2.  (  
  3.    [id] [intNULL,  
  4.    [name] [varchar](50) NULL,  
  5.   [isselectd] [intNULL  
  6.  )   
Adding a LINQ to SQL Class

Step 1


Right-click on the project and select "Add new item", then select Data from the templates.

Step 2

Choose "LINQ to SQL classes" from the list and provide a name. Now after clicking on Add, you can see the .dbml file in the project.

Step 3

Drag the department tables from the database in the Server Explorer.



Create model class with name college
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace mvcmodelfuncation.Models  
  7. {  
  8.     public class college  
  9.     {  
  10.         public string selectdepartment { setget; }  
  11.           public   List<department> depatrmts  
  12.         {  
  13.             get {   
  14.               
  15.                 DataClasses1DataContext db = new  DataClasses1DataContext();  
  16.                 return db.departments.ToList();   
  17.             }  
  18.           
  19.         }  
  20.     }  
  21. }  
In the preceding code we have two properties, selectdepartment and depatrmts. The depatrmts table is a list type, it gets the lists of departments from the database.

Create home controller
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using mvcmodelfuncation.Models;  
  7.   
  8.   
  9. namespace mvcmodelfuncation.Controllers  
  10. {  
  11.     public class homeController : Controller  
  12.     {  
  13.         //  
  14.         // GET: /home/  
  15.   
  16.         DataClasses1DataContext db = new DataClasses1DataContext();  
  17.         
  18.         [HttpGet]  
  19.         public ActionResult deprt()  
  20.         {  
  21.             college cc = new college();  
  22.            return View(cc);  
  23.           
  24.         }  
  25.         [HttpPost]  
  26.         public string  deprt(college cs)  
  27.         {  
  28.             if (string.IsNullOrEmpty(cs.selectdepartment))  
  29.             {  
  30.   
  31.                 return "you did not select and department";  
  32.             }  
  33.             else  
  34.             {  
  35.                 return "you selected department with id =" + cs.selectdepartment;  
  36.               
  37.             }  
  38.         }  
  39.   
  40.   
  41.     }  
  42. }  
In the preceding code we have two Actionresult methods, drpart and drpart (college cs), drpart is a [httpget] type and drpart (college cs) is a [httppost].

Create a view for showing the radiobuttonlist

Right-click on the Index ActionResult method and select Add View. After selecting Add View, a dialog box will open. The view name is deprt by default. Now select your model class and click on the OK button.

  1. @model mvcmodelfuncation.Models.college  
  2.   
  3. @{  
  4.     ViewBag.Title = "deprt";  
  5. }  
  6.   
  7. <h2>deprt</h2>  
  8.   
  9. @using (Html.BeginForm())  
  10. {  
  11.   
  12.     foreach (var depart in  Model.depatrmts)  
  13.     {   
  14.       
  15.     @Html.RadioButtonFor(m=>m.selectdepartment,depart.id)  
  16.     @depart.name  
  17.     <br />  
  18.      
  19.     }  
  20.     <br/>  
  21.     <br/>  
  22.     <input  type="submit" value="submit"/>  
  23.   
  24. }  
The following is the output of the preceding code.


If we will click submit button without select any radiobutton let's see the output.

If we will click the submit button with select any radiobutton let's see the output.



 
Arrange the RadioButton vertically 
  1. @model mvcmodelfuncation.Models.college  
  2.   
  3. @{  
  4.     ViewBag.Title = "deprt";  
  5. }  
  6.   
  7. <h2>deprt</h2>  
  8.   
  9. @using (Html.BeginForm())  
  10. {  
  11.   
  12.     foreach (var depart in  Model.depatrmts)  
  13.     {   
  14.       
  15.     @Html.RadioButtonFor(m=>m.selectdepartment,depart.id)  
  16.     @depart.name  
  17.     
  18.     <br/>
  19.     }  
  20.     <br/>  
  21.     <br/>  
  22.     <input  type="submit" value="submit"/>  
  23.   
  24. }  
 The following is the output of the preceding code.

Summary

In this article we learned how to use a RadioButtonList control in MVC using a HTML helper and binding with LINQ to SQL and arrange the RadioButtonList vertically and horizontally.