Radio button allows user to select one option from series of choices. In this example I will show you how to generate a radiobuttonlist in mvc using Html.RadioButtonFor.
We will be using City table
Step 1: Create a table using the following script with data:
USE[ShoppingZone]
GO
/******Object: Table [dbo].[City] Script Date: 04/22/2016 18:22:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[City](
[CityId] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[IsSelected] [bit] NOT NULL,
CONSTRAINT[PK_City] PRIMARY KEYCLUSTERED
(
[CityId] ASC
)WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[City]ON
INSERT [dbo].[City] ([CityId], [Name], [IsSelected]) VALUES (1, N'Mumbai', 0)
INSERT [dbo].[City] ([CityId], [Name], [IsSelected]) VALUES (2, N'London', 1)
INSERT [dbo].[City] ([CityId], [Name], [IsSelected]) VALUES (3, N'New York', 0)
INSERT [dbo].[City] ([CityId], [Name], [IsSelected]) VALUES (4, N'brussels', 0)
INSERT [dbo].[City] ([CityId], [Name], [IsSelected]) VALUES (5, N'sydney', 0)
INSERT [dbo].[City] ([CityId], [Name], [IsSelected]) VALUES (6, N'Riyadh', 0)
INSERT [dbo].[City] ([CityId], [Name], [IsSelected]) VALUES (7, N'seoul', 0)
SET IDENTITY_INSERT [dbo].[City]OFF
Step 1: Create an ado.net entity data model using table City and generate entity for that.
Step 2: Create a class and name it as mycity. Copy and paste the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVC_tutorials.Models
{
public class MyCity
{
public string SelectedCity { get; set; }
public List<City> Cities
{
get
{
models db = new models();
return db.Cities.ToList();
}
}
}
}
Step 3: Right click on the "Controllers" folder and add "RadioButton" controller. Copy and paste the following code. Please make sure to include "MVC_tutorials.Models" namespace.
using MVC_tutorials.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVC_tutorials.Controllers
{
public class RadioButtonController : Controller
{
// GET: /RadioButton/
[HttpGet]
public ActionResult Index()
{
MyCity mycity = new MyCity();
return View(mycity);
}
[HttpPost]
public string Index(MyCity mycity)
{
if (string.IsNullOrEmpty(mycity.SelectedCity))
{
return "You did not select any city";
}
else
{
return "You selected city with ID = " + mycity.SelectedCity;
}
}
}
}
Step 4: Right click on the "Index" action method in the " RadioButtonController" and add "Index" view. Copy and paste the following code.
@model MVC_tutorials.Models.MyCity
@{
ViewBag.Title = "Generatingradiobuttonlist in mvc";
}
<h2>Generating radiobuttonlist in mvc </h2>
@using (Html.BeginForm())
{
foreach (var city in Model.Cities)
{
@Html.RadioButtonFor(m => m.SelectedCity,city.CityId) @city.Name
}
<br />
<br />
<input type="submit" value="Submit" />
}
Run the application when you selected a city and click the submit button you will get a message “Selected city Id”. If you selected nothing you will get a message “you have not selected any city”.
Output: