Introduction
We can show and hide the modal using the methods provided by Bootstrap Modal plugin.
Description
We are using modal ('show') and modal ('hide') methods to show and hide the login modal.
Reference
Before this article you should go through my Introduction to Bootstrap. Show Bootstrap Image Using Asp.Net MVC http://www.c-sharpcorner.com/blogs/bootstrap-image-show-using-asp-net-mvc3
Steps
Create a controller class file called "HomeController.cs" and add controller action method called "ModalPopUp()";
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace SatyaMVCBootstrapImages.Controllers
- {
- public class HomeController : Controller
- {
-
-
-
- public ActionResult Index()
- {
- return View();
- }
-
- public ActionResult ModalPopUp()
- {
- return View();
- }
-
- }
- }
Then create a view file named "ModalPopUp.cshtml".
Here all reference files like bootstrap.min.css and jquery.min.js and bootstrap.min.js should be added to
add bootstrap features.
I have added code for a button to perform modal popup.
- <button id="btnShowModal" type="button"
- class="btn btn-sm btn-default pull-left col-lg-11 button button4">
- Satya Modals
- </button>
I have added code for the modal form to appear in a fade style.
- <div class="modal fade" tabindex="-1" id="loginModal"
- data-keyboard="false" data-backdrop="static">
Here is the code for size of the modal.
- <div class="modal-dialog modal-lg">
Controls are added Inside Modal content such as header text , buttons, cross symbol button, text boxes and related control properties:
- <div class="modal-content">
- <div class="modal-header">
- <button type="button" class="close" data-dismiss="modal">
- ×
- </button>
- <h4 class="modal-title">Satya Login</h4>
- </div>
- <div class="modal-body">
- <form>
- <div class="form-group">
- <input class="form-control" type="text"
- placeholder="Login Username" id="inputUserName" />
- </div>
- <div class="form-group">
- <input class="form-control" placeholder="Login Password"
- type="password" id="inputPassword" />
- </div>
- </form>
- </div>
- <div class="modal-footer">
- <button type="submit" class="btn btn-primary button button4">Sign</button>
- <button type="button" id="btnHideModal" class="btn btn-primary button button4">
- Hide
- </button>
- </div>
- </div>
For cross symbol button in header part.
- <button type="button" class="close" data-dismiss="modal">
- ×
- </button>
For Header part.
- <div class="modal-header">
- <button type="button" class="close" data-dismiss="modal">
- ×
- </button>
- <h4 class="modal-title">Satya Login</h4>
- </div>
For textBoxes
- <div class="modal-body">
- <form>
- <div class="form-group">
- <input class="form-control" type="text"
- placeholder="Login Username" id="inputUserName" />
- </div>
- <div class="form-group">
- <input class="form-control" placeholder="Login Password"
- type="password" id="inputPassword" />
- </div>
- </form>
- </div>
For footer part.
- <div class="modal-footer">
- <button type="submit" class="btn btn-primary button button4">Sign</button>
- <button type="button" id="btnHideModal" class="btn btn-primary button button4">
- Hide
- </button>
- </div>
Here I added one javascript and added button id such as btnShowModal and btnHideModal to show and hide modal.
- <script type="text/javascript">
- $(document).ready(function () {
- $("#btnShowModal").click(function () {
- $("#loginModal").modal('show');
- });
-
- $("#btnHideModal").click(function () {
- $("#loginModal").modal('hide');
- });
- });
- </script>
Here I added css style to add style in buttons.
- <style>
- .button {
- background-color: #4CAF50;
- border: none;
- color: white;
- padding: 15px 32px;
- text-align: center;
- text-decoration: none;
- display: inline-block;
- font-size: 16px;
- margin: 4px 2px;
- cursor: pointer;
- }
-
- .button1 {
- border-radius: 2px;
- }
-
- .button2 {
- border-radius: 4px;
- }
-
- .button3 {
- border-radius: 8px;
- }
-
- .button4 {
- border-radius: 12px;
- }
-
- .button5 {
- border-radius: 50%;
- }
- </style>
OUTPUT
Desktop View
By clicking hide button Or Cross Mark on header it will be hidden.
Mobile View
SUMMARY
We learned:
What is Modal Popup in bootstrap?
How to get it with Asp.net MVc.