In this article I will explain with an example, how to implement Bootstrap Modal Popup Form in ASP.Net Core MVC.
The Bootstrap Modal Popup Form will be used to save (insert) data into SQL Server Database using Entity Framework in ASP.Net Core MVC.
Note: For beginners in ASP.Net MVC Core and Entity Framework, please refer my article ASP.Net Core: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework with ASP.Net Core.
 
 
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 MVC: Bootstrap Modal Popup Form
 
I have already inserted few records in the table.
ASP.Net Core MVC: Bootstrap Modal Popup Form
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
Model
Following class is used as the Model class.
public class Customer
{
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
}
 
 
Creating an Entity Data Model
The very first step is to create an ASP.Net MVC Application and connect it to the Database using Entity Framework.
Once the Entity Framework is configured and connected to the database table, the Database Context will look as shown below.
using Bootstrap_Popup_EF_Insert_MVC_Core.Models;
using Microsoft.EntityFrameworkCore;
 
namespace Bootstrap_Popup_EF_Insert_MVC_Core
{
    public class DBCtx : DbContext
    {
        public DBCtx(DbContextOptions<DBCtx> options) : base(options)
        {
        }
 
        public DbSet<Customer> Customers { get; set; }
    }
}
 
 
Controller
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
 
Action method for handling POST operation
The Action method for POST operation accepts an object of the Customer Model class as parameter. The values posted from the Form inside the View 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.
The Customer Model object is returned back to the View.
public class HomeController : Controller
{
    private DBCtx Context { get; }
    public HomeController(DBCtx _context)
    {
        this.Context = _context;
    }
 
    public IActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public IActionResult Index(Customer customer)
    {
        this.Context.Customers.Add(customer);
        this.Context.SaveChanges();
 
        //Fetch the CustomerId returned via SCOPE IDENTITY.
        int id = customer.CustomerId;
 
        return View(customer);
    }
}
 
 
View
Inside the View, in the very first line the Customer Model class is declared as Model for the View.
Bootstrap Modal Popup
The View consists of a Bootstrap Modal Popup which is triggered (opened) using a Button.
 
Form
The Bootstrap Modal Popup consists of an HTML Form which has been created using the Razor Tag attributes with the following attributes.
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.
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.
After the Form is submitted, the Customer Model object returned from the Controller is checked for NULL and if it is not NULL then the newly inserted CustomerId is displayed using JavaScript Alert MessageBox.
@model Bootstrap_Popup_EF_Insert_MVC_Core.Models.Customer
@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" asp-action="Index" asp-controller="Home">
        <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="Name" class="form-control"/>
                        </div>
                        <div class="form-group">
                            <label>Country:</label>
                            <select asp-for="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">Save changes</button>
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    @if (Model != null)
    {
        <script type="text/javascript">
            $(function () {
                alert("Inserted Customer ID: " + @Model.CustomerId);
            });
        </script>
    }
</body>
</html>
 
 
Screenshots
The Form
ASP.Net Core MVC: Bootstrap Modal Popup Form
 
CustomerId displayed after data insert
ASP.Net Core MVC: Bootstrap Modal Popup Form
 
 
Downloads