0
1

public class ForgottenLoginModel
{
    [Display(Name = "Email address")]
    [Required(ErrorMessage = "The email address is required")]
    [EmailAddress(ErrorMessage = "Invalid Email Address")]
    public string Email { get; set; }
}

public ActionResult LoginDisplay()
{
    LoginPartialViewModel modelCollection = new LoginPartialViewModel();
    modelCollection.mLogin = new LoginModel(); 
    modelCollection.mForgottenLogin = new ForgottenLoginModel();

    return PartialView("_LoginDisplay", modelCollection);
}

[HttpPost]
[ValidateAjax]
public JsonResult SendUserCredentials(ForgottenLoginModel data)
{
    bool l_result = false;

    l_result = FindUserByEmail();

    return Json(new { result = l_result }, JsonRequestBehavior.AllowGet);
}

@model Nuclei.ViewModels.LoginPartialViewModel
<div id="ForgotLogin" class="box" style="border: none;">
    @using (Html.BeginForm("SendUserCredentials", "Login", FormMethod.Post, new { id = "ForgotLoginForm" }))
    {
        @Html.AntiForgeryToken()
        <div class="form-group">
            <label class="control-label" style="color:white;">
                @Html.DisplayNameFor(model => model.mForgottenLogin.Email)
            </label>
            @Html.EditorFor(model => model.mForgottenLogin.Email, new { htmlAttributes = new { id = "txtEmail", @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.mForgottenLogin.Email, "", new { @class = "text-danger" })
        </div>
        <div class="text-center" style="margin-top:40px;">
            <input type="button" id="BackToLoginBtn" value="Back" class="btn btn-default" />
            <input type="button" id="ForgotSubmitBtn" value="Submit" class="btn btn-primary" />
        </div>
    }
</div>

<script>
    $(document).ready(function () {
        function showForgottenProcessing(form) {
            var frmValues = form.serialize();
            $.ajax({
                type: form.attr('method'),
                url: form.attr('action'),
                data: frmValues,
                success: function (data) {
                    if (data.result == false) {
                        //Show error couldn't find an account with the email address
                        showForgotLoginForm();
                    }
                    else {
                        //On success go to the login form
                        showManualForm();
                    }
                },
                error: function (data) {
                    //Show errors if BadRequest else show error as dialog
                }
            });
        }

        $("#ForgotSubmitBtn").click(function () {
            showForgottenProcessing();
            e.preventDefault();
        });
    })
</script>

public class ValidateAjaxAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (!filterContext.HttpContext.Request.IsAjaxRequest())
            return;

        var modelState = filterContext.Controller.ViewData.ModelState;
        if (!modelState.IsValid)
        {
            var errorModel =
                    from x in modelState.Keys
                    where modelState[x].Errors.Count > 0
                    select new
                    {
                        key = x,
                        errors = modelState[x].Errors.
                                               Select(y => y.ErrorMessage).
                                               ToArray()
                    };
            filterContext.Result = new JsonResult()
            {
                Data = errorModel
            };
            filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
        }
    }
}
|
  • 1