Advertisement
Guest User

Untitled

a guest
May 15th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1. public class BaseController : Controller
  2. {
  3.     public JsonResult CreateResponse(object data, bool allowGet)
  4.     {
  5.         return Json(data, allowGet);
  6.     }
  7.     public JsonResult CreateResponse(object data)
  8.     {
  9.         return Json(data, JsonRequestBehavior.AllowGet);
  10.     }
  11. }
  12.  
  13. public class HomeController : BaseController
  14. {
  15.     [HttpGet]
  16.     public JsonResult Login(LoginUserModel model)
  17.     {
  18.         using (JeritEntities db = new JeritEntities())
  19.         {
  20.             try
  21.             {
  22.                 if (DoLogin(model.EmailAddress, model.Password)
  23.                 {
  24.                     return CreateResponse(true)
  25.                 }
  26.                 return CreateResponse(Json(new { IsOk = false, Title = "Log In", Message = "Invalid user credentials" }));
  27.             }
  28.             catch (Exception ex)
  29.             {
  30.                 // just report the error
  31.                 return CreateResponse(Json(new { IsOk = false, Title = "Log In", Message = ex.Message }));
  32.             }
  33.         }
  34.     }
  35. }
  36.  
  37. // here follows the view ~/Index.cshtml
  38.  
  39. <main class="mdl-layout__content">
  40.     <div class="page-content">
  41.         @RenderBody()
  42.  
  43.         <dialog class="mdl-dialog">
  44.             <h4 class="mdl-dialog__title"></h4>
  45.             <div class="mdl-dialog__content">
  46.                 <p></p>
  47.             </div>
  48.             <div class="mdl-dialog__actions">
  49.                 <button type="button" class="mdl-button ok">OK</button>
  50.                 <button type="button" class="mdl-button close">Close</button>
  51.             </div>
  52.         </dialog>
  53.     </div>
  54. </main>
  55. <script>
  56.     $("a.login").on("click", function () {
  57.         Login();
  58.     });
  59.  
  60.     function Login() {
  61.         $("#p2").show(); // indicate work in progress
  62.         var EmailAddress = $("#EmailAddress").val();
  63.         var Password = $("#Password").val();
  64.  
  65.         console.log("Email Address " + EmailAddress);
  66.         console.log("Password " + Password);
  67.  
  68.         var User = {
  69.             "EmailAddress": EmailAddress, "Password": Password
  70.         };
  71.         $.post("/Home/Login", User, function (data) {
  72.             if (!data) {
  73.                 $("#p2").hide();
  74.                 console.log("Title: " + data.Title); // value undefined
  75.                 console.log("Message: " + data.Message); // value undefined
  76.                 showMessage(data.Title, data.Message);
  77.             }
  78.             else
  79.             {
  80.                 window.location.href = '@Url.Content("~/")';
  81.             }
  82.         });
  83.     }
  84.  
  85.     function showMessage(title, msg) {
  86.         var dialog = document.querySelector('dialog');
  87.         if (!dialog.showModal) {
  88.             dialogPolyfill.registerDialog(dialog);
  89.         }
  90.         $(".mdl-dialog__title").html(title);
  91.         $(".mdl-dialog__content p").html(msg);
  92.         dialog.querySelector('.ok').addEventListener('click', function () {
  93.             dialog.close();
  94.         });
  95.         dialog.querySelector('.close').addEventListener('click', function () {
  96.             dialog.close();
  97.         });
  98.         $("button.close").hide();
  99.  
  100.         dialog.showModal();
  101.     }
  102. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement