Smart Alert With MVC - Part 2

I have already written an article about Sweet Alert, which is also used to display alert in ways other than the traditional one. You can have a look at it here:

Smart Alert with MVC

Let’s start with creating a basic MVC project, and then we are going to add and use Smart Alert in it.

Creating Application

Open Visual Studio IDE on start page and select New Project.

Start page
                                                              Figure 1. Start page

After selecting New Project, a dialog will appear. Inside that select Templates, then under Visual C# select Web. After selecting you will see various Project Templates (Webforms , MVC , ASP.NET AJAX Server control). Select “ASP.NET MVC 4 Web Application ” and name your project “SmartAlertSite” and finally click onthe OK button to create project.

Selecting Template
                                                           Figure 2. Selecting Template

After clicking on the OK button another project template selection wizard will pop up with the name “New ASP.NET MVC 4 Project." In this template, select Basic template and set view engine as Razor. We are not going to create Unit testing for this project, hence do not check this option, and finally click on the OK button and it is done.

Selecting MVC 4 Project
                                          Figure 3. Selecting MVC 4 Project

After selecting all the options as mentioned above now click the OK button and your project will be created.

Project structure
               Figure 4. Project structure

Downloading package of Smart alert and adding to project

To download the Smart Slert Script and css folder to add in the project just download from the following URL.

After downloading zip file extract it and add these files to MVC project.

Folder

Add a folder in the SmartAlertSite project with the name “SmartAlertFiles”. Inside this we are going to add all files which we have downloaded.

Adding Smart folder and files to project
Figure 5. Adding Smart folder and files to project

After adding “SmartAlertFiles” folder.

SmartAlertFiles
Figure 6. After adding Smart folder and files to project

After adding Bootstrap to theSmartAlertSite Project now we are going to add Model in Models folder.

Adding Model (OrderFood)

For adding model in Solution Explorer just right click on the Model folder and select Add, then inside that select Class and then Name class as OrderFood and finally click on Add button to create Model.

OrderFood
Adding Property to (OrderFood) Model

After adding model let us add property to this Model.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Linq;  
  5. using System.Web; 
  6.  
  7. namespace SmartAlertSite.Models  
  8. {  
  9.     public class OrderFood  
  10.     {  
  11.         [Key]  
  12.         public int OrderID  
  13.         {  
  14.             get;  
  15.             set;  
  16.         }  
  17.         [Required(ErrorMessage = "Please enter OrderCode")]  
  18.         public string OrderCode  
  19.         {  
  20.             get;  
  21.             set;  
  22.         }  
  23.         [Required(ErrorMessage = "Please enter OrderPrice")]  
  24.         public string OrderPrice  
  25.         {  
  26.             get;  
  27.             set;  
  28.         }  
  29.     }  
  30. }  
Adding (Home) Controller

To add controller just right click on the Controllers folder and then select Add from list and inside that select Controller.

Adding Controller
                                                   Figure 7. Adding Controller (Home Controller)

After selecting controller a new dialog will pop up with name Add Controller.

pop up with name Add Controller

Now let’s name Controller to DemoHomeController.

Inthe  template we are going to select Empty MVC controller.

Finally, click on the Add button to create DemoHomeController.

After adding DemoHomeController you will see Index Action Method created by default.

Adding 2 New Action Method with name created to DemoHomeController


Let’s add two new Actions with names created; one for POST and the other for Get.
  1. using SmartAlertSite.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7. namespace SmartAlertSite.Controllers  
  8. {  
  9.     public class DemoHomeController: Controller  
  10.     {  
  11.         //  
  12.         // GET: /DemoHome/  
  13.         public ActionResult Index()  
  14.         {  
  15.             return View();  
  16.         }  
  17.   
  18.         [HttpGet]  
  19.         public ActionResult Create()  
  20.         {  
  21.             return View(new OrderFood());  
  22.         }  
  23.         [HttpPost]  
  24.         public ActionResult Create(OrderFood OrderFood)  
  25.         {  
  26.             if (ModelState.IsValid)  
  27.             {}  
  28.             else  
  29.             {}  
  30.             return View(OrderFood);  
  31.         }  
  32.     }  
  33. }  
After adding Action Method now let us Add View to this Action Method.

Adding View for Create Action Method


For Adding View just right click inside Controller Action Method (Create) and then select option Add View.

DemoHome Controller
         Figure 8. Adding View (Create [DemoHome Controller])

After selecting Add View option a new wizard will pop up with Name Add View.

It will have View Name with the same name as Action Method inside which you right click to add View.
In this example we are going to create a strongly-typed view; for that I have checked this option and in Model class I have selected OrderFood model class.

After selectingthe Required option now click on Add button.

After that a View will be created inside the Views folder and in that we will have a folder with the name of controller. Your View will be placed inside of that.

After adding Create View
Figure 9. After adding Create View

Now let’s add Script Reference to this page to use Smart Alert.

Adding Script Reference to View (Create View)

Scripts to add for using smart alert
                                                  Figure 10. Scripts to add for using smart alert
  1. <link href="~/SmartAlertFiles/alert/css/alert.min.css" rel="stylesheet" />  
  2.   
  3. <link href="~/SmartAlertFiles/alert/themes/dark/theme.min.css" rel="stylesheet" />  
  4.   
  5. <script src="~/Scripts/jquery-1.7.1.min.js"></script>  
  6.   
  7. <script src="~/Scripts/jquery.validate.min.js"></script>  
  8. <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>  
  9.   
  10. <script src="~/SmartAlertFiles/alert/js/alert.js"></script>  
After adding Script now let’s add validation.

Adding Smart Alert Simple alert Validation on Create View

We are going to validate 2 textboxes (and they must not be blank) using Smart Alert.

JavaScript alert Smart alert
alert("Please enter OrderCode !"); $.alert.open('Please enter OrderCode !');
alert Smart alert
Alert starts with alert keyword. Smart Alert starts with $.alert.Open keyword.

                                 Figure 11. Different between Smart alert and JavaScript alert

Code snippet of Sweet Alert Validation on Create View

  1. <script type="text/javascript">  
  2.   
  3.     function validateData() {  
  4.         if ($("#OrderCode").val() == "") {  
  5.             $.alert.open('Please enter OrderCode !');  
  6.             return false;  
  7.         }  
  8.         else if ($("#OrderPrice").val() == "") {  
  9.             $.alert.open('Please enter OrderPrice !');  
  10.             return false;  
  11.         }  
  12.         else {  
  13.             return true;  
  14.         }  
  15.     }  
  16.   
  17. </script>  
  18. <input id="btnCreate" type="button" onclick="return validateData();" value="Create" />  
Screenshot of Smart Alert Validation on Create View

Smart alert Error message
                                   Figure 12. Smart alert Error message

Code snippet of Complete Create View

Here is the complete code snippet:
  1. @model SmartAlertSite.Models.OrderFood  
  2. @  
  3. {  
  4.     Layout = null;  
  5. }   
  6. < !DOCTYPE html > < html > < head > < meta name = "viewport" content = "width=device-width" / >   
  7. < title > Create < /title> < link href = "~/SmartAlertFiles/alert/css/alert.min.css" rel = "stylesheet" / >   
  8. < link href = "~/SmartAlertFiles/alert/themes/dark/theme.min.css" rel = "stylesheet" / >   
  9. < script src = "~/Scripts/jquery-1.7.1.min.js" > < /script> < script src = "~/Scripts/jquery.validate.min.js" > < /script>   
  10. < script src = "~/Scripts/jquery.validate.unobtrusive.min.js" > < /script>   
  11. < script src = "~/SmartAlertFiles/alert/js/alert.js" > < /script>   
  12. < script type = "text/javascript" > function validateData()  
  13. {  
  14.     if ($("#OrderCode").val() == "")  
  15.     {  
  16.         $.alert.open('Please enter OrderCode !');  
  17.         return false;  
  18.     }  
  19.     else if ($("#OrderPrice").val() == "")  
  20.     {  
  21.         $.alert.open('Please enter OrderPrice !');  
  22.         return false;  
  23.     }  
  24.     else  
  25.     {  
  26.         return true;  
  27.     }  
  28. } < /script> < /head> < body > @using(Html.BeginForm())  
  29. {  
  30.     @Html.ValidationSummary(true) < fieldset > < legend > OrderFood < /legend> < div class = "editor-label" > @Html.LabelFor(m => m.OrderCode) < /div> < div class = "editor-field" > @Html.EditorFor(m => m.OrderCode)  
  31.     @Html.ValidationMessageFor(m => m.OrderCode) < /div> < div class = "editor-label" > @Html.LabelFor(m => m.OrderPrice) < /div> < div class = "editor-field" > @Html.EditorFor(m => m.OrderPrice)  
  32.     @Html.ValidationMessageFor(m => m.OrderPrice) < /div> < p > < input id = "btnCreate"  
  33.     type = "button"  
  34.     onclick = "return validateData();"  
  35.     value = "Create" / > < /p> < /fieldset>  
  36. } < div > @Html.ActionLink("Back to List""Index") < /div> < /body> < /html>  
After adding Simple Validation now let us learn how to add Confirmation message in Smart Alert.

Adding Smart Alert Confirmation Validation

We have already finished simple Smart Alert validations, and now let’s work on Confirmation validation. We are going to call this Confirmation Alert as the user clicks on the save button and we are going to ask, "Do you want to save form?" There will be two buttons: [Save] and [Cancel]. If he/she clicks on Save, then form will be submitted, else it will be cancelled.

Adding (Confirmation) Controller

For adding the Confirmation message let us add another Controller for understanding with the name “Confirmation Controller.” In this controller we are going to add two Action Methods with the same name, Create.

Adding Confirmation Controller
                                   Figure 13. Adding Confirmation Controller

Adding View for Create Action Method

After adding Action methods now let’s add View to these action methods in similar way as we have added above.

For Adding View just right click inside Controller Action Method (Create) and then select option Add View.

Adding View of Confirmation Controller
      Figure 14. Adding View (Create) of Confirmation Controller

After selecting the Add View option a new wizard will pop up with Name (Add View).

It will have a View Name with the same name as Action Method inside which right click to add View.

In this example we are going to create a strongly typed view. For that I have checked this option and in Model class I have selected OrderFood model class.

After selectingthe required option now finally click on the Add button. After that a View will be created inside the Views folder, and in that we will have a folder with the name of the controller, inside which your view will be placed.

Adding Script Reference to View (Create View)
  1. <link href="~/SmartAlertFiles/alert/css/alert.min.css" rel="stylesheet" />  
  2.   
  3. <link href="~/SmartAlertFiles/alert/themes/dark/theme.min.css" rel="stylesheet" />  
  4.   
  5. <script src="~/Scripts/jquery-1.7.1.min.js"></script>  
  6.   
  7. <script src="~/Scripts/jquery.validate.min.js"></script>  
  8. <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>  
  9.   
  10. <script src="~/SmartAlertFiles/alert/js/alert.js"></script>  
After adding script now let’s add validation.

Adding Smart Alert Confirmation Validation on Create View

This script will first ask “Do you want to save it?" then there are 2 buttons “Save” and “Cancel”. If you click on Save then it will be saved else it will be cancelled.
  1. <script type="text/javascript">  
  2.     function validateData()  
  3.     {  
  4.         if ($("#OrderCode").val() == "")  
  5.         {  
  6.             $.alert.open('Please enter OrderCode !');  
  7.             return false;  
  8.         }  
  9.         else if ($("#OrderPrice").val() == "")  
  10.         {  
  11.             $.alert.open('Please enter OrderPrice !');  
  12.             return false;  
  13.         }  
  14.         else  
  15.         {  
  16.             return true;  
  17.         }  
  18.     }  
  19.   
  20.     function Validate()  
  21.     {  
  22.         $.alert.open('confirm''Are you sure you want to save this data?', function(button)  
  23.         {  
  24.             if (button == 'yes')  
  25.             {  
  26.                 if (validateData() == true)  
  27.                 {  
  28.                     $("#CreateForm").submit();  
  29.                 }  
  30.             }  
  31.             else if (button == 'no')  
  32.             {  
  33.                 $.alert.open('You pressed the "No" button.');  
  34.             }  
  35.             else  
  36.             {  
  37.                 $.alert.open('Alert was canceled.');  
  38.             }  
  39.         });  
  40.     }  
  41. </script>  
  42. <input id="btnCreate" type="button" onclick="return Validate();" value="Create" />  
Screenshot of Smart Alert Confirmation onclick of Create button

Smart Alert Confirmation onclick
                               Figure 15. Smart Alert Confirmation onclick of Create button

Screnshot of Smart Alert Confirmation on Cancellation:

Alert Confirmation on Cancellation

Code snippet of Complete Create View of (Confirmation)

  1. @model SmartAlertSite.Models.OrderFood  
  2. @  
  3. {  
  4.     Layout = null;  
  5. < !DOCTYPE html > < html > < head > < meta name = "viewport"  
  6. content = "width=device-width" / > < title > Create < /title> 
  7. < link href = "~/SmartAlertFiles/alert/css/alert.min.css"  rel = "stylesheet" / > 
  8. < link href = "~/SmartAlertFiles/alert/themes/dark/theme.min.css"  rel = "stylesheet" / > 
  9. < script src = "~/Scripts/jquery-.7.1.min.js" > < /script> 
  10. < script src = "~/Scripts/jquery.validate.min.js" > < /script> 
  11. < script src = "~/Scripts/jquery.validate.unobtrusive.min.js" > < /script> 
  12. < script src = "~/SmartAlertFiles/alert/js/alert.js" > < /script> <
  13.  script type = "text/javascript" > function validateData()  
  14. {  
  15.     if ($("#OrderCode").val() == "")  
  16.     {  
  17.         $.alert.open('Please enter OrderCode !');  
  18.         return false;  
  19.     }  
  20.     else if ($("#OrderPrice").val() == "")  
  21.     {  
  22.         $.alert.open('Please enter OrderPrice !');  
  23.         return false;  
  24.     }  
  25.     else  
  26.     {  
  27.         return true;  
  28.     }  
  29. }  
  30.   
  31. function Validate()  
  32. {  
  33.     $.alert.open('confirm''Are you sure you want to save this data?', function(button)  
  34.     {  
  35.         if (button == 'yes')  
  36.         {  
  37.             if (validateData() == true)  
  38.             {  
  39.                 $("#CreateForm").submit();  
  40.             }  
  41.         }  
  42.         else if (button == 'no')  
  43.         {  
  44.             $.alert.open('You pressed the "No" button.');  
  45.         }  
  46.         else  
  47.         {  
  48.             $.alert.open('Alert was canceled.');  
  49.         }  
  50.     });  
  51. } < /script> < /head> < body > @using(Html.BeginForm(nullnull, FormMethod.Post, new  
  52. {  
  53.     id = "CreateForm"  
  54. }))  
  55. {  
  56.     @Html.ValidationSummary(true) < fieldset > < legend > OrderFood < /legend> < div class = "editor-label" > @Html.LabelFor(model => model.OrderCode) < /div> < div class = "editor-field" > @Html.EditorFor(model => model.OrderCode)  
  57.     @Html.ValidationMessageFor(model => model.OrderCode) < /div> < div class = "editor-label" > @Html.LabelFor(model => model.OrderPrice) < /div> < div class = "editor-field" > @Html.EditorFor(model => model.OrderPrice)  
  58.     @Html.ValidationMessageFor(model => model.OrderPrice) < /div> < p > < input id = "btnCreate"  
  59.     type = "button"  
  60.     onclick = "return Validate();"  
  61.     value = "Create" / > < /p> < /fieldset>  
  62. } < /body> < /html>  
After adding the Confirmation on the Create button now let us move forward to add Confirmation while deleting records.

Adding (DeleteConfirmation) Controller

To add a Controller just right click on the Controller folder and then select Add from list and inside that select controller.

After selecting Controller a new dialog will pop up withthe name Add Controller.

Adding DeleteConfirmationController
                           Figure 16. Adding DeleteConfirmationController

After adding Controller we are going to add two Action Methods in this controller. One will return View with List and other will return JSON result.

Code snippet of DeleteConfirmation Controller
  1. public class DeleteConfirmationController: Controller  
  2. {  
  3.     [HttpGet]  
  4.     public ActionResult Details()  
  5.     {  
  6.         List < OrderFood > objlistorderfd = new List < OrderFood > ();  
  7.         OrderFood OF1 = new OrderFood();  
  8.         OF1.OrderID = 1;  
  9.         OF1.OrderCode = "FD0001";  
  10.         OF1.OrderPrice = "2000";  
  11.         OrderFood OF2 = new OrderFood();  
  12.         OF1.OrderID = 2;  
  13.         OF1.OrderCode = "FD0002";  
  14.         OF1.OrderPrice = "600";  
  15.         OrderFood OF3 = new OrderFood();  
  16.         OF1.OrderID = 3;  
  17.         OF1.OrderCode = "FD0003";  
  18.         OF1.OrderPrice = "3000";  
  19.         objlistorderfd.Add(OF1);  
  20.         objlistorderfd.Add(OF2);  
  21.         objlistorderfd.Add(OF3);  
  22.         return View(objlistorderfd);  
  23.     }  
  24.     public ActionResult Delete(string OrderID)  
  25.     {  
  26.         return Json("Delete", JsonRequestBehavior.AllowGet);  
  27.     }  
  28. }  
Code snippet of DeleteConfirmation Controller

After adding Action Method now we are going to add View to this Action Method (Details).

For adding View just right click inside Controller Action Method (Create) and then select option Add View.

Adding View
Figure 17. Adding View with name Details [DeleteConfirmationController]

After selecting Add View option a new wizard will pop up with the name Add View.

Inthe Scaffold template I have selected List because we are going to show the list of order. From that you can delete if you don’t want any.

It will have View Name with the same name as Action Method inside which you right click to add View.
In this example we are going to create strongly typed view and for that I have checked this option and in Model class I have selected OrderFood model class.

After selecting the required option now finally click on the Add button.

After that a View will be created inside the Views folder and in that we will have a folder with name of controller. Inside that your view will be placed.

Making changes in View removing link button and adding Single button for Delete

We have added a List page here and it will show all Order records:

View Details
                              Figure 18. View Details [DeleteConfirmationController]

From Details View we are going to remove this link button and going to add a single button here.

View Details after removing action links
              Figure 19. View Details after removing action links and adding single delete button

After adding a button now we are going to add Scripts Reference.

Adding Script Reference to View (Details View)
  1. <link href="~/SmartAlertFiles/alert/css/alert.min.css" rel="stylesheet" />  
  2. <link href="~/SmartAlertFiles/alert/themes/dark/theme.min.css" rel="stylesheet" />  
  3.   
  4. <script src="~/Scripts/jquery-1.7.1.min.js"></script>  
  5. <script src="~/Scripts/jquery.validate.min.js"></script>  
  6. <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>  
  7. <script src="~/SmartAlertFiles/alert/js/alert.js"></script>  
After adding script now let’s add validation.

This script will first ask “Are you sure that you want to delete this Order?”,  then there are 2 buttons “Delete” and “Cancel”. If you click on Delete button then it will be delete record using Ajax Post request, otherwise if you clicked on theCancel button then it does not perform anything. 
  1. It will show dialog of confirmation.

  2. If you click on the delete button then it will perform Ajax Post event with this URL. [/DeleteConfirmation/delete/] and pass OrderID to it for deleting record. After that it will call [window.location.href = '/DeleteConfirmation/Details'] ( to refresh this page ).

Code snippet of deleteorder function

  1. <script type="text/javascript">  
  2.     function deleteOrder(OrderID)  
  3.     {  
  4.         $.alert.open('confirm''Are you sure that you want to delete this Order?', function(button)  
  5.         {  
  6.             if (button == 'yes')  
  7.             {  
  8.                 $.ajax(  
  9.                 {  
  10.                     url: "/DeleteConfirmation/delete/",  
  11.                     data:  
  12.                     {  
  13.                         "OrderID": OrderID  
  14.                     },  
  15.                     type: "DELETE",  
  16.                     success: function(data)  
  17.                     {  
  18.                         if (data == 'delete')  
  19.                         {  
  20.                             if (button == 'ok')  
  21.                             {  
  22.                                 window.location.href = '/DeleteConfirmation/Details';  
  23.                             }  
  24.                         }  
  25.                     }  
  26.                 });  
  27.             }  
  28.             else if (button == 'no')  
  29.             {  
  30.                 $.alert.open('You pressed the "No" button.');  
  31.             }  
  32.             else  
  33.             {  
  34.                 $.alert.open('Alert was canceled.');  
  35.             }  
  36.         });  
  37.     }  
  38. </script>  
Screenshot of Smart Alert DeleteConfirmation on click of Delete button

Confirmation while deleting records
                                          Figure 20. Confirmation while deleting records

Code snippet of Details View
  1. @model IEnumerable < SmartAlertSite.Models.OrderFood > @  
  2. {  
  3.     Layout = null;  
  4. } < !DOCTYPE html > < html > < head > < meta name = "viewport"  
  5. content = "width=device-width" / > < title > Details < /title> < link href = "~/SmartAlertFiles/alert/css/alert.min.css"  
  6. rel = "stylesheet" / > < link href = "~/SmartAlertFiles/alert/themes/dark/theme.min.css"  
  7. rel = "stylesheet" / > < script src = "~/Scripts/jquery-1.7.1.min.js" > < /script> < script src = "~/Scripts/jquery.validate.min.js" > < /script> < script src = "~/Scripts/jquery.validate.unobtrusive.min.js" > < /script> < script src = "~/SmartAlertFiles/alert/js/alert.js" > < /script> < script type = "text/javascript" > function deleteOrder(OrderID)  
  8. {  
  9.     $.alert.open('confirm''Are you sure that you want to delete this Order?', function(button)  
  10.     {  
  11.         if (button == 'yes')  
  12.         {  
  13.             $.ajax(  
  14.             {  
  15.                 url: "/DeleteConfirmation/delete/",  
  16.                 data:  
  17.                 {  
  18.                     "OrderID": OrderID  
  19.                 },  
  20.                 type: "DELETE",  
  21.                 success: function(data)  
  22.                 {  
  23.                     if (data == 'delete')  
  24.                     {  
  25.                         if (button == 'ok')  
  26.                         {  
  27.                             window.location.href = '/DeleteConfirmation/Details';  
  28.                         }  
  29.                     }  
  30.                 }  
  31.             });  
  32.         }  
  33.         else if (button == 'no')  
  34.         {  
  35.             $.alert.open('You pressed the "No" button.');  
  36.         }  
  37.         else  
  38.         {  
  39.             $.alert.open('Alert was canceled.');  
  40.         }  
  41.     });  
  42. } < /script> < /head> < body > < p style = "text-align:left" > Delete Order < /p> < table style = "margin-left:20px;" > < tr > < th style = "text-align:center" > @Html.DisplayNameFor(model => model.OrderCode) | < /th>  < th style = "text-align:center" > @Html.DisplayNameFor(model => model.OrderPrice) | < /th>  < th > Action < /th> < /tr>   
  43. @foreach(var item in Model)  
  44. { < tr > < td style = "text-align:center" > @Html.DisplayFor(modelItem => item.OrderCode) < /td> < td style = "text-align:center" > @Html.DisplayFor(modelItem => item.OrderPrice) < /td> < td style = "text-align:center" > < input id = "Delete"  
  45.     onclick = "deleteOrder('@item.OrderID')"  
  46.     type = "button"  
  47.     value = "Delete" / > < /td> < /tr>  
  48. } < /table> < /body> < /html>  
Finally, we are going to see how to show a message after Saving Records.

Displaying Success Message after Saving Record

I have made a small change inthe Http Post request of the create action method of the home controller.

If thedata submitted is valid, then I am going to show a message. For that I have stored a message in TempData to display, and after this I am redirecting the request to Http Get request of create action method of the home controller.
  1. public class DemoHomeController: Controller  
  2. {  
  3.     [HttpGet]  
  4.     public ActionResult Create()  
  5.     {  
  6.         return View(new OrderFood());  
  7.     }  
  8.     [HttpPost]  
  9.     public ActionResult Create(OrderFood OrderFood)  
  10.     {  
  11.         if (ModelState.IsValid)  
  12.         {  
  13.             TempData["Message"] = "Your Order " + OrderFood.OrderCode + "has been Saved Successfully ";  
  14.             return RedirectToAction("Create");  
  15.         }  
  16.         else  
  17.         {}  
  18.         return View(OrderFood);  
  19.     }  
  20. }  
Code snippet of Create View [DemoHome Controller]

In this code snippet of Create View of the home controller, I am going to check that TempData["Message"] is not null and if it is not null, then I am going to show the message which is stored in TempData using Smart Alert.
  1. <link href=”~/SmartAlertFiles/alert/css/alert.min.css” rel=”stylesheet” />  
  2. <link href=”~/SmartAlertFiles/alert/themes/dark/theme.min.css” rel=”stylesheet” />  
  3. <script src=”~/Scripts/jquery-1.7.1.min.js”></script>  
  4. <script src=”~/Scripts/jquery.validate.min.js”></script>  
  5. <script src=”~/Scripts/jquery.validate.unobtrusive.min.js”></script>  
  6. <script src=”~/SmartAlertFiles/alert/js/alert.js”></script>  
  7. <script type=”text/javascript”>  
  8.     @if(TempData[“Message”] != null)  
  9.     { < text > $(window).load(function()  
  10.         {  
  11.             $(document).ready(function()  
  12.             {  
  13.                 $.alert.open(‘@TempData[“Message”]’);  
  14.             });  
  15.         }); < /text>  
  16.     }  
  17. </script>  
Screenshot of Smart Alert on Saving Record.

Message after saving record
                                               Figure 21. Message after saving record

Finally we have completed using Smart Alert with MVC.


Similar Articles