In this article I will explain with an example, how to implement
Bootstrap Modal Popup Form in
ASP.Net MVC Razor.
Database
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Entity Framework Model
Once the
Entity Framework is configured and connected to the database table, the Model will look as shown below.
Controller
The Controller consists of following 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 handle 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.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Customer customer)
{
using (CustomerEntities entities = new CustomerEntities())
{
entities.Customers.Add(customer);
entities.SaveChanges();
int id = customer.CustomerId;
}
return View(customer);
}
}
View
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.
Form
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>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
body {padding: 50px; }
</style>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorig in="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorig in="anonymous" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorig in="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">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>Name:</label>
@Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
</div>
<div class="form-group">
<label>Country:</label>
@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>
</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>
}
@if (Model != null)
{
<script type="text/javascript">
$(function () {
alert("Inserted Customer ID: " + @Model.CustomerId);
});
</script>
}
</body>
</html>
Screenshots
The Form
CustomerId displayed after data insert
Downloads