Advertisement
rohitkandhal

Reusable Modal Dialogs

Mar 28th, 2014
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.15 KB | None | 0 0
  1. // STEP 1
  2. public class DeleteConfirmationViewModel
  3. {
  4.     /// <summary>
  5.     /// Action to perform after user confirms "Delete".
  6.     /// </summary>
  7.     public string PostDeleteAction { get; set; }
  8.  
  9.     /// <summary>
  10.     /// [OPTIONAL] Controller to look for Post Delete action.
  11.     /// This controller has implementation of Post Delete action
  12.     /// </summary>
  13.     public string PostDeleteController { get; set; }
  14.  
  15.     /// <summary>
  16.     /// While executing POST Delete action, we need id of entity to delete.
  17.     /// </summary>
  18.     public int DeleteEntityId { get; set; }
  19.  
  20.     /// <summary>
  21.     /// Delete Confirmation dialog header text. For example
  22.     /// For text like "Delete Estimated Effort", Header Text is "Estimated Effort"
  23.     /// </summary>
  24.     public string HeaderText { get; set; }
  25. }
  26.  
  27. // STEP 2: Add HTTPGET and HTTPPOST methods in TestController class
  28. public class TestController
  29. {
  30.     [HttpGet]
  31.     public ActionResult DeleteItem(int itemId = 0)
  32.     {
  33.        
  34.         // Create new instance of DeleteConfirmationViewModel and pass it to _DeleteConfirmation Dialog (Reusable)
  35.         DeleteConfirmationViewModel deleteConfirmationViewModel = new DeleteConfirmationViewModel
  36.         {
  37.             DeleteEntityId = itemId,
  38.             HeaderText = "Test Item",
  39.             PostDeleteAction = "DeleteItem",
  40.             PostDeleteController = "Test"
  41.             // This method is in MyController class
  42.         };
  43.         return PartialView("_DeleteConfirmation", deleteConfirmationViewModel);
  44.     }
  45.  
  46.     [HttpPost]
  47.     public ActionResult DeleteItem(DeleteConfirmationViewModel deleteConfirmationViewModel)
  48.     {
  49.         // Get item which we need to delete from EntityFramework
  50.         Item item = db.Items.Find(deleteConfirmationViewModel.DeleteEntityId);
  51.  
  52.         if (item == null)
  53.         {
  54.             return HttpNotFound();
  55.         }
  56.  
  57.         if (ModelState.IsValid)
  58.         {
  59.             try
  60.             {
  61.                 db.Items.Remove(item);
  62.                 db.SaveChanges();
  63.                 return Json(new { success = true });
  64.             }
  65.             catch (Exception e)
  66.             {
  67.                 ModelState.AddModelError("", e.Message);
  68.             }
  69.         }
  70.         return PartialView("_DeleteConfirmation", deleteConfirmationViewModel);
  71.     }
  72. }
  73.  
  74. // STEP 3: Add View in View/Shared folder with filename: _DeleteConfirmation.cshtml
  75.  
  76. @model LMSPriorTool.ViewModels.DeleteConfirmationViewModel
  77.  
  78. <div class="modal-header">
  79.     <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
  80.     <h3>Delete @Html.DisplayFor(x => x.HeaderText)</h3>
  81. </div>
  82.  
  83. @using (Html.BeginForm(Model.PostDeleteAction, Model.PostDeleteController, FormMethod.Post, new { @class = "modal-form form-horizontal" }))
  84. {
  85.     @Html.ValidationSummary()
  86.     <!-- Added hidden field so that these values persist when calling POST Action. Otherwise
  87.     POST action will have all these properties as null -->
  88.     @Html.HiddenFor( x => x.DeleteEntityId)
  89.     @Html.HiddenFor( x => x.PostDeleteAction)
  90.     @Html.HiddenFor( x => x.PostDeleteController)
  91.     @Html.HiddenFor( x => x.HeaderText) <!-- Though not required in post action but may be in case of error useful-->
  92.     <div class="modal-body">
  93.         <div class="row-fluid">
  94.             <div class="control-group">
  95.                 Do you want to delete this entry?
  96.             </div>
  97.         </div>
  98.     </div>
  99.    
  100.     <div class="modal-footer">
  101.         <button class="btn btn-danger" type="submit"><i class="icon-trash icon-white"></i> Delete</button>
  102.         <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
  103.     </div>
  104. }
  105.  
  106.  
  107. // STEP 4: In your shared layout file present in "Views/Shared" folder add this:
  108. <!DOCTYPE html>
  109. <html lang="en">
  110. <head>
  111.     <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
  112.     <meta charset="UTF-8" />
  113.     <meta name="viewport" content="width=device-width" />
  114.     <link href="@Styles.Url("~/content/css")" rel="Stylesheet" />
  115.     <script src="@Url.Content("~/Scripts/jquery-1.9.1.min.js")" type="text/javascript"></script>
  116. </head>
  117. <body>
  118.     <div class="navbar navbar-fixed-top">
  119.         <div class="navbar-inner">
  120.             <div class="container">
  121.                 <div class="row">
  122.                     @Html.ActionLink("Delete", "DeleteItem", "Test",
  123.                         new { itemId = 1 },
  124.                         new { id = "btndelete", @class = "btn btn-danger modal-link" })
  125.                 </div>
  126.             </div>
  127.         </div>
  128.     </div>
  129.    
  130.     <!-- Modal Dialog Content -->
  131.     <div id="dialogDiv" class="modal hide fade in">
  132.         <div id="dialogContent">
  133.         </div>
  134.     </div>
  135.     <!-- Javascript: Placed at the end of the document -->
  136.     @*@Scripts.Render("~/bundles/jquery")*@
  137.     @Scripts.Render("~/bundles/jqueryui")
  138.     @Scripts.Render("~/bundles/modernizr")
  139.     @Scripts.Render("~/bundles/knockout")
  140.     @Scripts.Render("~/js")
  141.     @RenderSection("scripts", required: false)
  142. </body>
  143. </html>
  144.  
  145.  
  146.  
  147. // STEP 6: Update your project javascript file
  148. Test.js
  149.  
  150. $(function () {
  151.     $('.modal-link').click(function () {
  152.         $('#dialogContent').load(this.href, function () {
  153.             $('#dialogDiv').modal({
  154.                 backdrop: 'static',
  155.                 keyboard: true
  156.             }, 'show');
  157.             bindForm(this);
  158.         });
  159.         return false;
  160.     });
  161. });
  162.  
  163. function bindForm(dialog) {
  164.     $('form', dialog).submit(function () {
  165.         $.ajax({
  166.             url: this.action,
  167.             type: this.method,
  168.             data: $(this).serialize(),
  169.             success: function (result) {
  170.                 if (result.success) {
  171.                     $('#dialogDiv').modal('hide');
  172.                     // Refresh:
  173.                     location.reload();
  174.                 } else {
  175.                     $('#dialogContent').html(result);
  176.                     bindForm();
  177.                 }
  178.             }
  179.         });
  180.         return false;
  181.     });
  182. }
  183.  
  184.  
  185.  
  186.  
  187. // Step 7: Updated your bundle config file (AppData)
  188. // This is a verbose config file that I currently have
  189.  
  190. public class BundleConfig
  191. {
  192.     public static void RegisterBundles(BundleCollection bundles)
  193.     {
  194.         bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
  195.                     "~/Scripts/jquery-{version}.js"));
  196.  
  197.         bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
  198.                     "~/Scripts/jquery-ui-{version}.js"));
  199.  
  200.         bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
  201.                     "~/Scripts/jquery.unobtrusive*",
  202.                     "~/Scripts/jquery.validate*"));
  203.  
  204.         bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
  205.                     "~/Scripts/modernizr-*"));
  206.  
  207.         bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
  208.                     "~/Content/themes/base/jquery.ui.core.css",
  209.                     "~/Content/themes/base/jquery.ui.resizable.css",
  210.                     "~/Content/themes/base/jquery.ui.selectable.css",
  211.                     "~/Content/themes/base/jquery.ui.accordion.css",
  212.                     "~/Content/themes/base/jquery.ui.autocomplete.css",
  213.                     "~/Content/themes/base/jquery.ui.button.css",
  214.                     "~/Content/themes/base/jquery.ui.dialog.css",
  215.                     "~/Content/themes/base/jquery.ui.slider.css",
  216.                     "~/Content/themes/base/jquery.ui.tabs.css",
  217.                     "~/Content/themes/base/jquery.ui.datepicker.css",
  218.                     "~/Content/themes/base/jquery.ui.progressbar.css",
  219.                     "~/Content/themes/base/jquery.ui.theme.css"));
  220.  
  221.         bundles.Add(new ScriptBundle("~/bundles/knockout").Include("~/Scripts/knockout-2.1.0.js"));
  222.         bundles.Add(new StyleBundle("~/Content/css").Include(
  223.             "~/Content/bootstrap.css",
  224.             "~/Content/bootstrap-responsive.css",
  225.             "~/Content/LMSStyle.css",
  226.             "~/Content/bootstrap-rowlink.css"
  227.             ));
  228.  
  229.         bundles.Add(new ScriptBundle("~/js").Include(
  230.             "~/Scripts/bootstrap.js",
  231.             "~/Scripts/bootstrap-rowlink.js",
  232.             "~/Scripts/Test.js"
  233.             ));
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement