In this article I will explain with an example, how to save (insert) selected (checked) RadioButton value from the RadioButtonList (list of RadioButtons) to database using Entity Framework in ASP.Net MVC Razor.
Multiple RadioButtons (RadioButtonList) will also be populated with selected value from database using Entity Framework in ASP.Net MVC Razor.
 
 
Database
I have made use of the following table Hobbies with the schema as follows.
Save (Insert) RadioButton value to database in ASP.Net MVC
 
I have already inserted few records in the table.
Save (Insert) RadioButton value to database in ASP.Net MVC
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
Entity Framework Model
Once the Entity Framework is configured and connected to the database table, the Model will look as shown below.
Note: For beginners in ASP.Net MVC and Entity Framework, please refer my article ASP.Net MVC: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework.
 
Save (Insert) RadioButton value to database in ASP.Net MVC
 
 
Controller
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, the records from the Hobbies table are fetched using Entity Framework 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.
Note: This example uses Model class object for capturing Form field values, for more details please refer my article ASP.Net MVC: Form Submit (Post) example.
 
When the Form is submitted, the posted values are captured through an Integer variable which contains the value of the selected (checked) RadioButton from the list of RadioButtons.
Then a loop is executed over the Hobby Entity Model class objects and the IsSelected column value of the Hobbies are updated into the Database using Entity Framework.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        HobbiesEntities entities = new HobbiesEntities();
        return View(entities.Hobbies.ToList());
    }
 
    [HttpPost]
    public ActionResult Save(int hobby)
    {
        HobbiesEntities entities = new HobbiesEntities();
        foreach (Hobby m in entities.Hobbies)
        {
            if (m.HobbyId == hobby)
            {
                m.IsSelected = true;
            }
            else
            {
                m.IsSelected = false;
            }
        }
 
        entities.SaveChanges();
 
        return RedirectToAction("Index");
    }
}
 
 
View
Inside the View, in the very first line the Entity Framework Hobby Model class is declared as Model for the View.
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case the name is Save.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
Inside the Form, a loop is executed over the Generic List of Entity Framework Hobby Model class objects and an HTML Table is generated consisting of a RadioButton, and a Label.
When the Submit Button is clicked, the Form gets submitted and the value of the selected (checked) RadioButton is sent to the Controller.
@model List<RadioButtonList_Insert_Database_MVC.Hobby>
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
    <style type="text/css">
        body {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
</head>
<body>
    @using (Html.BeginForm("Save", "Home", FormMethod.Post))
    {
        <table>
            @foreach (var m in Model)
            {
                <tr>
                    <td>
                        @Html.RadioButton("Hobby", m.HobbyId, m.IsSelected, new { @id = m.HobbyId })
                    </td>
                    <td>
                        @Html.Label(m.HobbyName, new { @for = m.HobbyId })
                    </td>
                </tr>
            }
        </table>
        <br/>
        <input type="submit" value="Save"/>
    }
</body>
</html>
 
 
Screenshot
Save (Insert) RadioButton value to database in ASP.Net MVC
 
 
Downloads