In this article I will explain with an example, how to populate (bind) RadioButtonList from database using Entity Framework in ASP.Net Core Razor Pages.
 
 
Database
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
ASP.Net Core Razor Pages: Populate (Bind) RadioButtonList from Database using Entity Framework
 
I have already inserted few records in the table.
ASP.Net Core Razor Pages: Populate (Bind) RadioButtonList from Database using Entity Framework
 
Note: You can download the database table SQL by clicking the download link below.
        Download SQL file
 
 
Model
The Model class consists of the following two properties.
public class Customer
{
    public int 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.
Note: For beginners in ASP.Net Core and Entity Framework, please refer my article ASP.Net Core Razor Pages: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework with ASP.Net Core.
 
using Microsoft.EntityFrameworkCore;
 
namespace RadioButtonList_EF_Razor_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 System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Rendering;
 
 
Razor PageModel (Code-Behind)
The PageModel consists of following two Handler methods.
Handler method for handling GET operation
Inside this Handler method, the GetCustomers method is called and assigned to the public property Customers.
Inside the GetCustomers method, the records from the Customers table are fetched using Entity Framework and copied into generic list of SelectListItem class object.
Finally, the public property Customers is returned to the Razor Page.
 
Handler method for handling Button Click and POST operation
This Handler method handles the POST call when the Submit Button is clicked and the Form is submitted.
When the Form is submitted, the CustomerId of selected Customer is captured in the customer parameter.
Finally, the GetCustomers method is called and the selected Customer is fetched using Lambda expression and it is set into a ViewData object which will be later displayed in Razor Page using JavaScript Alert Message Box.
public class IndexModel : PageModel
{
    private DBCtx Context { get; }
    public IndexModel(DBCtx _context)
    {
        this.Context = _context;
    }
 
    public List<SelectListItem> Customers { get; set; }
 
    public void OnGet()
    {
        this.Customers = GetCustomers();
    }
 
    public void OnPostSubmit(string customer)
    {
        this.Customers = GetCustomers();
        var selectedCustomer = this.Customers.Find(p => p.Value == customer);
        if (selectedCustomer != null)
        {
            selectedCustomer.Selected = true;
            ViewData["Message"] = "Selected Customer: " + selectedCustomer.Text;
        }
    }
 
    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;
    }
}
 
 
Razor Page (HTML)
The HTML of Razor Page consists of an HTML Form with a Submit Button.
Inside the Form, a loop is executed over the generic List of Customer Model 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.
 
The Submit Button has been set with the POST Handler method using the asp-page-handler attribute.
Note: In the Razor PageModel, the Handler method name is OnPostSubmit but here it will be specified as Submit when calling from the Razor HTML Page.
 
When the Submit Button is clicked, the Form gets submitted and the value of the selected RadioButton is sent to the PageModel.
Finally, the selected Customer name is displayed using JavaScript Alert Message Box.
@page
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model RadioButtonList_EF_Razor_Core.Pages.IndexModel
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form method="post">
        @foreach (var customer in Model.Customers)
        {
            <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" asp-page-handler="Submit" />
        @if (ViewData["Message"] != null)
        {
            <script type="text/javascript">
                 window.onload = function () {
                    alert("@ViewData["Message"]");
                };
            </script>
        }
    </form>
</body>
</html>
 
 
Screenshot
ASP.Net Core Razor Pages: Populate (Bind) RadioButtonList from Database using Entity Framework
 
 
Downloads