In this article I will explain with an example, how to insert DropDownList selected value in SQL Server Database in ASP.Net Core Razor Pages.
The selected value of the DropDownList will be inserted into SQL Server 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: Insert DropDownList selected value in Database
 
I have already inserted few records in the table.
ASP.Net Core Razor Pages: Insert DropDownList selected value in Database
 
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 three properties.
public class Customer
{
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public string Country { 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 Bootstrap_Popup_EF_Insert_Razor_Core
{
    public class DBCtx : DbContext
    {
        public DBCtx(DbContextOptions<DBCtx> options) : base(options)
        {
        }
 
        public DbSet<Customer> Customers { get; set; }
    }
}
 
 
Razor PageModel (Code-Behind)
The PageModel consists of following two Handler methods.
Handler method for handling GET operation
This Handler method handles the GET calls, for this particular example it is not required and hence left empty.
 
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.
This Handler method accepts an object of the Customer Model class as parameter. The values posted from the Form inside the Razor Page are received through this parameter.
The received values are then inserted into the SQL Server database table using Entity Framework.
The CustomerId of the inserted record is available in the Customer Model object after the SaveChanges method is executed.
Finally, the Customer Model object is set into the Customer public property.
public class IndexModel : PageModel
{
    private DBCtx Context { get; }
    public IndexModel(DBCtx _context)
    {
        this.Context = _context;
    }
 
    public Customer Customer { get; set; }
 
    public void OnGet()
    {
    }
 
    public void OnPostSubmit(Customer customer)
    {
        this.Context.Customers.Add(customer);
        this.Context.SaveChanges();
 
        //Fetch the CustomerId returned via SCOPE IDENTITY.
        int id = customer.CustomerId;
 
        this.Customer = customer;
    }
}
 
 
Razor Page (HTML)
Inside the Razor Page, the ASP.Net TagHelpers is inherited.
Bootstrap Modal Popup
The Razor Page consists of a Bootstrap Modal Popup which is triggered (opened) using a Button.
 
Form
The Bootstrap Modal Popup consists of an HTML Form.
There is one HTML TextBox field created for capturing value for Name. While for capturing the Country value, a HTML DropDownList (Select) with Country options is created.
Finally, the Bootstrap Modal Popup consists of two Buttons i.e. a Submit Button to Submit the Form and a Button to close the Bootstrap Modal Popup.
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.
 
After the Form is submitted, the Customer Model object returned from the IndexModel is checked for NULL and if it is not NULL then the newly inserted CustomerId is displayed using JavaScript Alert MessageBox.
@page
@model Bootstrap_Popup_EF_Insert_Razor_Core.Pages.IndexModel
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" />
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
 
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">Insert</button>
    <form method="post">
        <div id="exampleModal" class="modal" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Customer Details Form</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label>Name:</label>
                            <input type="text" asp-for="Customer.Name" class="form-control" />
                        </div>
                        <div class="form-group">
                            <label>Country:</label>
                            <select asp-for="Customer.Country" class="form-control">
                                <option value="">Please select</option>
                                <option value="India">India</option>
                                <option value="China">China</option>
                                <option value="Australia">Australia</option>
                                <option value="France">France</option>
                                <option value="Unites States">Unites States</option>
                                <option value="Russia">Russia</option>
                                <option value="Canada">Canada</option>
                            </select>
                        </div>
                        <div class="modal-footer">
                            <button type="submit" class="btn btn-primary" asp-page-handler="Submit">Save changes</button>
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    @if (Model.Customer != null)
    {
        <script type="text/javascript">
            $(function () {
                alert("Inserted Customer ID: " + @Model.Customer.CustomerId);
            });
        </script>
    }
</body>
</html>
 
 
Screenshots
The Form
ASP.Net Core Razor Pages: Insert DropDownList selected value in Database
 
CustomerId displayed after data insert
ASP.Net Core Razor Pages: Insert DropDownList selected value in Database
 
 
Downloads