Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Model
The Model class consists of the following properties.
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
Creating an Entity Data Model
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 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 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 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
HTML Markup
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.
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" 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>
<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">×</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>
<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>
</form>
@if (Model != null)
{
<script type="text/javascript">
$(function () {
alert("Inserted Customer ID: " + @Model.CustomerId);
});
</script>
}
</body>
</html>
Screenshot
Downloads