In this article I will explain with an example, how to bind
RadioButton (multiple RadioButtons) using Model in ASP.Net Core MVC.
The generic list collection of Model class will be populated from Database inside Controller and then it will be used to populate the list of multiple RadioButtons inside View in ASP.Net Core MVC.
Database
I have made use of the following table Customers with the schema as follow.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Model
The Model class consists of the following properties.
public class Customer
{
public string CustomerId { get; set; }
public string Name { get; set; }
}
Database Context
Once the
Entity Framework is configured and connected to the database table, the Database Context will look as shown below.
using RadioButtonList_EF_MVC_Core.Models;
using Microsoft.EntityFrameworkCore;
namespace RadioButtonList_EF_MVC_Core
{
public class DBCtx : DbContext
{
public DBCtx(DbContextOptions<DBCtx> options) : base(options)
{
}
public DbSet<Customer> Customers { get; set; }
}
}
Namespaces
You will need to import the following namespaces.
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
Controller
The Controller consists of following Action methods.
Action method for handling GET operation
Inside this Action method, the GetCustomers method is called.
Inside the
GetCustomers method, the records are fetched from the
Customers table using
Entity Framework and are copied into Generic list of
SelectListItem class and returned to the View.
Action method for handling POST operation
This Action method handles the call made from the POST function from the View when the Submit Button is clicked.
When the Form is submitted, the posted value is captured through the
customer parameter which is also set as the
Name of the
HTML RadioButtons.
Then the GetCustomers method is called and the selected Customer is fetched using Lambda expression.
Finally, the
Name of the selected
Customer is set into a
ViewBag object which will be later displayed in View using
JavaScript Alert Message Box.
public class HomeController : Controller
{
private DBCtx Context { get; }
public HomeController(DBCtx _context)
{
this.Context = _context;
}
public IActionResult Index()
{
return View(this.GetCustomers());
}
[HttpPost]
public IActionResult Index(string customer)
{
var selectedCustomer = this.GetCustomers().Find(p => p.Value == customer);
if (selectedCustomer != null)
{
selectedCustomer.Selected = true;
ViewBag.Message = "Selected Customer: " + selectedCustomer.Text;
}
return View(this.GetCustomers());
}
private List<SelectListItem> GetCustomers()
{
List<SelectListItem> customerList = (from p in this.Context.Customers
select new SelectListItem
{
Text = p.Name,
Value = p.CustomerId.ToString()
}).ToList();
return customerList;
}
}
View
HTML Markup
Inside the View, in the very first line the generic list of SelectListItem class is declared as model for the View.
The View consists of an HTML Form with following ASP.Net Tag Helpers attributes and a Submit Button.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
Inside the Form, ForEach Loop is executed over the generic List of
SelectListItem class which in turn generates a list containing a group of
HTML Label elements and
HTML RadioButtons.
Note: The RadioButtons are grouped by keeping the Name attribute value constant for all RadioButtons.
When the
Submit Button is clicked, the Form gets submitted and the value of the selected
RadioButton is sent to the Controller.
Finally, the selected Customer name is displayed using
JavaScript Alert Message Box.
@using RadioButtonList_EF_MVC_Core.Models;
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model List<SelectListItem>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post" asp-controller="Home" asp-action="Index">
@foreach (var customer in Model)
{
<input id="@customer.Value" type="radio" name="Customer" value="@customer.Value" />
<label for="@customer.Value">@customer.Text</label>
<br />
}
<br />
<input type="submit" value="Submit" />
</form>
@if (ViewBag.Message != null)
{
<script type="text/javascript">
window.onload = function() {
alert("@ViewBag.Message");
};
</script>
}
</body>
</html>
Screenshot
Downloads