Here We learn, how to Bind Radio Button List from database in ASP.NET MVC
Output:
HTML Helper class have two extension methods to generate radio button
input type="radio"
In a razor view:
1) @Html.RadioButton()
2) and @Html.RadioButtonFor()
RadioButtonFor:
RadioButtonFor helper method is a strongly typed extension method.
It generates input type="radio" element for the property specified using a lambda expression.
RadioButtonFor method binds a specified model object property to RadioButton control.
So it is automatically checked or unchecked a RadioButton based on the property value.
Model Class:
namespace MVC_Sample.Models
{
public class department
{
public List - SelectListItem - Dept { get; set; }
public string Name { get; set; }
public int DepartmentID { get; set; }
}
}
Bind from Models:
SelectListItem : Represents the selected item in an instance of the SelectList class.
@model MVC_Sample.Models.department
@foreach (var opt in Model.Dept)
{
div
@Html.RadioButtonFor(m =greater sign m.DepartmentID, opt.Value) @opt.Text
/div
}