Introduction To BootBox JavaScript Library In ASP.NET MVC

Introduction

What Is Bootbox In Asp.net Mvc?

Bootbox.js is a small JavaScript library which allows you to create programmatic dialog boxes, using Bootstrap modals, creating, managing or removing any of the required DOM elements or JS event handlers.

Bootbox.js is designed to make using Bootstrap modals easier.

Description

Note the order of the script references

Since Bootbox is a wrapper around Bootstrap's modal functionality, you need to include the libraries in order:

  1. jQuery
  2. Bootstrap
  3. Bootbox

For getting Bootbox script file reference Visit My Article

http://www.c-sharpcorner.com/article/google-map-location-coordinates-using-bootbox-in-asp-net-mvc/

Steps To Be Followed

Step1

Create a Mvc 5 application named “SatyaGoogleMapBootstrapMVC”.


Step2

Create a controller class file named “HomeController.cs”.

Code Ref

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace SatyaGoogleMapBootstrapMVC.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         public ActionResult BootBox()  
  12.         {  
  13.             return View();  
  14.         }  
  15.     }  

Code Description

Here I created a controller action method named “BootBox()”.

  1. public ActionResult BootBox()  
  2.         {  
  3.             return View();  
  4.         } 
 

Step3

Add a Bootbox Javascript library of version v4.4.0. Named “bootbox.min.js”. This file I added to show alert pop up instead of traditional javascript alert box.

Also it is bootstrap supporting and multiplatform view support.


Step4

Create a view named “BootBox.cshtml”.

Code Ref

  1. @{  
  2.     ViewBag.Title = "Satyaprakash BootBox Intro";  
  3. }  
  4.   
  5. <title>@ViewBag.Title</title>  
  6.   
  7. <h2 style="background-color: Yellow;color: Blue; text-align: center; font-style: oblique">Satyaprakash's BootBox Intro Using MVC and BOOTSTRAP</h2>  
  8.   
  9. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">  
  10. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  
  11. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
  12.   
  13.   
  14. <script src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.4.0/bootbox.min.js">  
  15. </script>  
  16.   
  17. <script>  
  18.     bootbox.prompt({  
  19.         title: "This is a prompt with a textarea!",  
  20.         inputType: 'textarea',  
  21.         callback: function (result) {  
  22.             bootbox.alert(result);  
  23.         }  
  24.     });  
  25. </script>  
  26.   
  27. <footer>  
  28.     <p style="background-color: Yellow; font-weight: bold; color:blue; text-align: center; font-style: oblique">© @DateTime.Now.ToLocalTime()</p> @*Add Date Time*@  
  29. </footer> 
Code Description

To support bootstrap related support added below links.

  1. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">  
  2. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  
  3. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
To show BootBox alert popup added this file path as described earlier.
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.4.0/bootbox.min.js">  
  2. </script> 
 OR/
  1. <script src="bootbox.min.js"></script>  

Instead of that I can use also below CDN link Wrappers for JavaScript alert(), confirm() and other flexible dialogs using the Bootstrap framework. CDN for web related libraries to speed up your websites!

  • https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.4.0/bootbox.min.js
  • https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.4.0/bootbox.js 
I added bootbox related script to prompt a text area and user what ever data will put that will show using bootbox alert.
  1. <script>  
  2.     bootbox.prompt({  
  3.         title: "This is a prompt with a textarea!",  
  4.         inputType: 'textarea',  
  5.         callback: function (result) {  
  6.             bootbox.alert(result);  
  7.         }  
  8.     });  
  9. </script> 
 For prompt
  1. bootbox.prompt 
For Alert
  1. bootbox.alert 
For Text Area
  1. title: "This is a prompt with a textarea!",  
  2. inputType: 'textarea'
I use callback function to get the result from text area.

A callback handles the user's selection. The callback function is passed a value of true or false, depending on which button the user pressed. Any code that depends on the user's selection must be placed in the callback function.

Prompt

A dialog prompts for user input. Pressing the ESC key or clicking close dismisses the dialog and invokes the callback as if the user had clicked the cancel button.

Prompt dialogs require a callback function.

A dialog prompts for user input. Pressing the ESC key or clicking close dismisses the dialog and invokes the callback as if the user had clicked the cancel button.

Prompt dialogs require a callback function.

All Bootstrap modals, unlike native alerts, confirms, or prompts, generate non-blocking events. Keep that in mind when using the prompt() dialog, as it is not a drop-in replacement for native prompt dialogs. Any code that depends on the user's input must be placed in the callback function.

Prompt requires the title option (when using the options object) and disallows the message option.

Prompt Dialog Options

Prompt dialogs are (by default) single-line text inputs. You can modify the type of prompt Bootbox generates using the prompt-only options below.

  • Name - value , Description : You can set the initial value of the prompt using the value option.
  • Name - inputType , Description : Changes the type of input generated for the prompt dialog. Valid values for inputType are: text (default), textarea, email, select, checkbox, date, time, number, and password.
  • Name - inputOptions , Description : If you specify select or checkbox as the input type, you must also supply an array of valid values in the format of : {text: '', value: '', group: '' }

group is an optional property for populating the <select>; if specified, <optgroup> elements will be generated for each group value found in the inputOptions array.

Example 1

  1. bootbox.confirm({  
  2.         message: "This is a confirm with custom button text and color! Do you like it?",  
  3.         buttons: {  
  4.             confirm: {  
  5.                 label: 'Yes',  
  6.                 className: 'btn-success'  
  7.             },  
  8.             cancel: {  
  9.                 label: 'No',  
  10.                 className: 'btn-danger'  
  11.             }  
  12.         },  
  13.         callback: function (result) {  
  14.             bootbox.alert('This was logged in the callback: ' + result);  
  15.         }  
  16.     }); 
URL IS - http://localhost:57237/Home/BootBox
 
OUTPUT 1

This Will Show you Yes /No Button and after click It will show you True / False.

Desktop View


 
 

 
 
 

Mobile View

Example 2
  1. bootbox.prompt({  
  2.         title: "This is a prompt with a password input!",  
  3.         inputType: 'password',  
  4.         callback: function (result) {  
  5.             bootbox.alert(result);  
  6.         }  
  7.     }); 
OUTPUT 2

This will show you a password textbox.

 
Example 3
  1. var dialog = bootbox.dialog({  
  2.         title: 'A custom dialog with init',  
  3.         message: '<p><i class="fa fa-spin fa-spinner"></i> Loading...</p>'  
  4.     });  
  5.     dialog.init(function () {  
  6.         setTimeout(function () {  
  7.             dialog.find('.bootbox-body').html('I was loaded after the dialog was shown!');  
  8.         }, 3000);  
  9.     }); 
OUTPUT 3

This will show you the dialog with loading and after a certain time span it will show you text.


 
 

 
Example 4
  1. bootbox.alert({  
  2.         message: "This is the small alert!",  
  3.         size: 'small'  
  4.     }); 
OUTPUT 4

This will show you small alert box.


 
Example 5
  1. bootbox.confirm({  
  2.       title: "Destroy planet?",  
  3.       message: "Do you want to activate the Deathstar now? This cannot be undone.",  
  4.       buttons: {  
  5.           cancel: {  
  6.               label: '<i class="fa fa-times"></i> Cancel'  
  7.           },  
  8.           confirm: {  
  9.               label: '<i class="fa fa-check"></i> Confirm'  
  10.           }  
  11.       },  
  12.       callback: function (result) {  
  13.           bootbox.alert('This was logged in the callback: ' + result);  
  14.       }  
  15.   }); 
OUTPUT 5

This will show you confirm dialog box and it will show true / false after click on buttons.


 
Example 6
  1. bootbox.prompt({  
  2.         title: "This is a prompt with a set of checkbox inputs!",  
  3.         inputType: 'checkbox',  
  4.         inputOptions: [  
  5.             {  
  6.                 text: 'Choice One',  
  7.                 value: '1',  
  8.             },  
  9.             {  
  10.                 text: 'Choice Two',  
  11.                 value: '2',  
  12.             },  
  13.             {  
  14.                 text: 'Choice Three',  
  15.                 value: '3',  
  16.             }  
  17.         ],  
  18.         callback: function (result) {  
  19.             bootbox.alert(result);  
  20.         }  
  21.     }); 
OUTPUT 6

It will show you checkboxes inside Bootbox prompt.


 
Example 7
  1. bootbox.prompt({  
  2.         title: "This is a prompt with a number input!",  
  3.         inputType: 'number',  
  4.         callback: function (result) {  
  5.             bootbox.alert(result);  
  6.         }  
  7.     }); 
OUTPUT 7

It will show you input-only support for numbers. After putting a number then click button it will show the input number by user.

 

Example 8

  1. bootbox.alert({  
  2.         size: "small",  
  3.         title: "My Name",  
  4.         message: "Satyaprakash Samantaray",  
  5.         callback: function (result) { bootbox.alert(result); }  
  6.     }); 

OUTPUT 8

It will show you the message with title.


 
Example 9
  1. bootbox.alert("My Nick Name Is : <b style='color:red'>KULU</b>"
OUTPUT 9

This will show with customized alert text.


 
Example 10
  1. bootbox.dialog({ message: '<div class="text-center"><i class="fa fa-refresh fa-spin" style="color:green;"></i> Loading...</div>' }) 
OUTPUT 10

It will show loading with dialog.


 
Gif For Better Idea

Code Ref

  1. bootbox.prompt({  
  2.       title: "Write Your Name",  
  3.       placeholder:"Type Here....",  
  4.       inputType: 'text',  
  5.       callback: function (result) {  
  6.           bootbox.alert(result);  
  7.       }  
  8.   }); 
 
 
 

Summary
  1. What is BootBox?
  2. How to implement in Mvc.
  3. Compare Normal javascript and BootBox Javascript Alert PopUp.
  4. Get bootbox reference CDN link.

Up Next
    Ebook Download
    View all
    Learn
    View all