Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BaseController : Controller
- {
- public JsonResult CreateResponse(object data, bool allowGet)
- {
- return Json(data, allowGet);
- }
- public JsonResult CreateResponse(object data)
- {
- return Json(data, JsonRequestBehavior.AllowGet);
- }
- }
- public class HomeController : BaseController
- {
- [HttpGet]
- public JsonResult Login(LoginUserModel model)
- {
- using (JeritEntities db = new JeritEntities())
- {
- try
- {
- if (DoLogin(model.EmailAddress, model.Password)
- {
- return CreateResponse(true)
- }
- return CreateResponse(Json(new { IsOk = false, Title = "Log In", Message = "Invalid user credentials" }));
- }
- catch (Exception ex)
- {
- // just report the error
- return CreateResponse(Json(new { IsOk = false, Title = "Log In", Message = ex.Message }));
- }
- }
- }
- }
- // here follows the view ~/Index.cshtml
- <main class="mdl-layout__content">
- <div class="page-content">
- @RenderBody()
- <dialog class="mdl-dialog">
- <h4 class="mdl-dialog__title"></h4>
- <div class="mdl-dialog__content">
- <p></p>
- </div>
- <div class="mdl-dialog__actions">
- <button type="button" class="mdl-button ok">OK</button>
- <button type="button" class="mdl-button close">Close</button>
- </div>
- </dialog>
- </div>
- </main>
- <script>
- $("a.login").on("click", function () {
- Login();
- });
- function Login() {
- $("#p2").show(); // indicate work in progress
- var EmailAddress = $("#EmailAddress").val();
- var Password = $("#Password").val();
- console.log("Email Address " + EmailAddress);
- console.log("Password " + Password);
- var User = {
- "EmailAddress": EmailAddress, "Password": Password
- };
- $.post("/Home/Login", User, function (data) {
- if (!data) {
- $("#p2").hide();
- console.log("Title: " + data.Title); // value undefined
- console.log("Message: " + data.Message); // value undefined
- showMessage(data.Title, data.Message);
- }
- else
- {
- window.location.href = '@Url.Content("~/")';
- }
- });
- }
- function showMessage(title, msg) {
- var dialog = document.querySelector('dialog');
- if (!dialog.showModal) {
- dialogPolyfill.registerDialog(dialog);
- }
- $(".mdl-dialog__title").html(title);
- $(".mdl-dialog__content p").html(msg);
- dialog.querySelector('.ok').addEventListener('click', function () {
- dialog.close();
- });
- dialog.querySelector('.close').addEventListener('click', function () {
- dialog.close();
- });
- $("button.close").hide();
- dialog.showModal();
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement