forum

Home / DeveloperSection / Forums / How to pass data from Action to Ajax success function in mvc4?

How to pass data from Action to Ajax success function in mvc4?

Gaurpriya Bishnoi 3092 06-Jul-2015
hi i am want to when login succesfully then call my success function otherwise call error function
View code here
<div class="container">
    <div class="login-container">
        <div class="avatar"><img src="@Url.Content("~/Content/images/download.jpeg")" style="max-width:95%;" /></div>
        <div class="form-box">
            @using (Html.BeginForm())
            {
                <div class="input-group">
                    <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                    @Html.TextBoxFor(m => m.UserId, new { @class = "form-control", @id="userid", @placeholder = "Username", @required = "required", @maxlength = "20" })
                </div>
                <div class="input-group">
                    <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                    @Html.PasswordFor(m => m.Password, new { @class = "form-control", @id = "userpass", @placeholder = "Password", @required = "required", @maxlength = "20" })
                </div>
                <button class="btn btn-info btn-block login" type="submit" id="login-btn"><i class="glyphicon glyphicon-log-in"></i>&nbsp;&nbsp;&nbsp;Login</button>
            }
        </div>
    </div>
</div>

ajax code here:
<script>
    $(document).ready(function () {
        $('#login-btn').click(function () {
            var dataObject = {
                Id: $("#userid").val(),
                Password: $("#userpass").val()
            };
            $.ajax({
                url: '@Url.Action("Login","Account")',
                type: "POST",
                data: dataObject,
                dataType: "json",
                success: function (data) {
                    
                    if (data.toString() == "login") {
                        toastr['success']("Login Successfully");
                    }
                    else if (data.toString() == "error") {
                        toastr['error']("Id or Password is incorrect");
                    }
                },
                error: function () {
                    toastr['error']("Hello");
                }
            });
            
        });
    });
</script>

Controller Code here:

 [HttpPost]
        public ActionResult Login(LoginMaster model)
        {
            string message = "";
            if (ModelState.IsValid)
            {
                try
                {
                    var user = from emp in db.LoginMasters
                               where emp.UserId == model.UserId && emp.Password == model.Password
                               select emp;
                    var rol = user.FirstOrDefault();
                    if (rol != null)
                    {
                        var realrol = rol.Role;
                        if (realrol == "admin")
                        {
                            message = "login";
                            return RedirectToAction("Index", "Home");
                        }
                        else if (realrol == "user")
                        {
                            Session["userid"] = rol.UserId;
                            message = "login";
                            return RedirectToAction("User", "Home");
                        }
                    }
                    else
                    {
                        message = "error";
                    }
                }
                catch (Exception ex)
                {
                    ViewBag.cath = ex.Message;
                }
            }
            else
            {
                message = "error";
            }
            if (Request.IsAjaxRequest())
            {
                return new JsonResult { Data = message, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            }
            return View();

I am want to when we login succesfully that time call this 
toastr['success']("Login Successfully");
and when login fail that time call
toastr['error']("Id or Password is incorrect");
please solve this problem.
thanks in advance!

Updated on 07-Jul-2015

Liked By