In this article I will explain with an example, how to implement Bootstrap Modal Popup Form in ASP.Net MVC Razor.
The Bootstrap Modal Popup Form will be used to save (insert) data into SQL Server Database using Entity Framework in ASP.Net MVC Razor.
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.
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
Bootstrap Modal Popup Form in ASP.Net MVC
I have already inserted few records in the table.
Bootstrap Modal Popup Form 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.
Bootstrap Modal Popup Form in ASP.Net MVC
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 Entity Framework 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 Entity Framework Customer Model object after the SaveChanges method is executed.
The Entity Framework Customer Model object is returned back to the View.
public class HomeController : Controller
    // GET: Home
    public ActionResult Index()
        return View();
    public ActionResult Index(Customer customer)
        using (CustomerEntities entities = new CustomerEntities())
            int id = customer.CustomerId;
        return View(customer);
Inside the View, in the very first line the Entity Framework 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.
The Bootstrap Modal Popup 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 Index.
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.
There is one TextBox field created for capturing value for Name using the Html.TextBoxFor method. While for capturing the Country value, a DropDownList with Country options is created using the Html.DropDownListFor function.
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 Entity Framework 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.Customer
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width"/>
    <script src="" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"/>
    <script src="" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">Insert</button>
    @using (Html.BeginForm("Index", "Home", FormMethod.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>
                    <div class="modal-body">
                        <div class="form-group">
                            @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
                        <div class="form-group">
                            @Html.DropDownListFor(m => m.Country, new List<SelectListItem>
                                   { new SelectListItem{Text="India", Value="India"},
                                     new SelectListItem{Text="China", Value="China"},
                                     new SelectListItem{Text="Australia", Value="Australia"},
                                     new SelectListItem{Text="France", Value="France"},
                                     new SelectListItem{Text="Unites States", Value="Unites States"},
                                     new SelectListItem{Text="Russia", Value="Russia"},
                                     new SelectListItem{Text="Canada", Value="Canada"}},
                                     "Please select", new { @class = "form-control" })
                    <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>
    @if (Model != null)
        <script type="text/javascript">
            $(function () {
                alert("Inserted Customer ID: " + @Model.CustomerId);
The Form
Bootstrap Modal Popup Form in ASP.Net MVC
CustomerId displayed after data insert
Bootstrap Modal Popup Form in ASP.Net MVC