Bootstrap Modal Popup Form in ASPNet MVC (2024)

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.

Database

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 ASPNet MVC (1)

I have already inserted few records in the table.

Bootstrap Modal Popup Form in ASPNet MVC (2)

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.

Bootstrap Modal Popup Form in ASPNet MVC (3)

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 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();

}

[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>

</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>

@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>

</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

Bootstrap Modal Popup Form in ASPNet MVC (4)

CustomerId displayed after data insert

Bootstrap Modal Popup Form in ASPNet MVC (5)

Downloads

Bootstrap Modal Popup Form in ASPNet MVC (2024)
Top Articles
Hedging Transaction: What it is, How it Works
Collaborate, Share Content, and Edit Documents - Dropbox
Mickey Moniak Walk Up Song
Dunhams Treestands
Skyward Houston County
Login Page
Fat People Falling Gif
Online Reading Resources for Students & Teachers | Raz-Kids
The 10 Best Restaurants In Freiburg Germany
Triumph Speed Twin 2025 e Speed Twin RS, nelle concessionarie da gennaio 2025 - News - Moto.it
St Als Elm Clinic
Www Craigslist Louisville
Ou Class Nav
Music Archives | Hotel Grand Bach - Hotel GrandBach
Vocabulario A Level 2 Pp 36 40 Answers Key
Prices Way Too High Crossword Clue
Fire Rescue 1 Login
Osrs Blessed Axe
Washington, D.C. - Capital, Founding, Monumental
People Portal Loma Linda
Justified Official Series Trailer
Toy Story 3 Animation Screencaps
Jet Ski Rental Conneaut Lake Pa
Ezel Detailing
Deshuesadero El Pulpo
Hctc Speed Test
Cfv Mychart
John Deere 44 Snowblower Parts Manual
Keshi with Mac Ayres and Starfall (Rescheduled from 11/1/2024) (POSTPONED) Tickets Thu, Nov 1, 2029 8:00 pm at Pechanga Arena - San Diego in San Diego, CA
Lilpeachbutt69 Stephanie Chavez
Emuaid Max First Aid Ointment 2 Ounce Fake Review Analysis
La Qua Brothers Funeral Home
MethStreams Live | BoxingStreams
Nail Salon Open On Monday Near Me
Luciipurrrr_
Police Academy Butler Tech
Space Marine 2 Error Code 4: Connection Lost [Solved]
Weapons Storehouse Nyt Crossword
Emerge Ortho Kronos
Mars Petcare 2037 American Italian Way Columbia Sc
Linda Sublette Actress
Taylor University Baseball Roster
Doordash Promo Code Generator
Best GoMovies Alternatives
Fool's Paradise Showtimes Near Roxy Stadium 14
Celsius Claims Agent
Movie Hax
Ihop Deliver
Rubmaps H
Southern Blotting: Principle, Steps, Applications | Microbe Online
Die 10 wichtigsten Sehenswürdigkeiten in NYC, die Sie kennen sollten
Latest Posts
Article information

Author: Moshe Kshlerin

Last Updated:

Views: 6216

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Moshe Kshlerin

Birthday: 1994-01-25

Address: Suite 609 315 Lupita Unions, Ronnieburgh, MI 62697

Phone: +2424755286529

Job: District Education Designer

Hobby: Yoga, Gunsmithing, Singing, 3D printing, Nordic skating, Soapmaking, Juggling

Introduction: My name is Moshe Kshlerin, I am a gleaming, attractive, outstanding, pleasant, delightful, outstanding, famous person who loves writing and wants to share my knowledge and understanding with you.