---
title: "How to pass data from Action to Ajax success function in mvc4?"  
description: "How to pass data from Action to Ajax success function in mvc4?"  
author: "Gaurpriya Bishnoi"  
published: 2015-07-06  
updated: 2026-05-15  
canonical: https://www.mindstick.com/forum/23323/how-to-pass-data-from-action-to-ajax-success-function-in-mvc4  
category: "ajax"  
tags: ["jquery", "javascript", "mvc"]  
reading_time: 6 minutes  

---

# How to pass data from Action to Ajax success function in mvc4?

hi i am want to when [login](https://www.mindstick.com/articles/12852/styles-login-form-in-android) succesfully then call my [success](https://www.mindstick.com/articles/12957/best-tips-to-make-your-ghostwriting-experience-a-success) [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) otherwise call [error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing) function**[View](https://yourviews.mindstick.com/view/84701/layoffs-in-google-india-2023-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](https://www.mindstick.com/articles/1541/check-availability-of-user-name-or-email-using-asp-dot-net-ajax-and-jquery) 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](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc) 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 calltoastr['error']("Id or [Password](https://www.mindstick.com/blog/118/retriving-user-password-by-using-stored-procedure-in-asp-dot-net) is incorrect");please [solve this problem](https://answers.mindstick.com/qa/94681/my-google-assistant-shows-completely-different-search-results-from-what-i-ask-how-can-i-solve-this-problem).thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)!

## Replies

### Reply by Gaurpriya Bishnoi

Thank you john smith

### Reply by John Smith

check following articles\
http://www.mindstick.com/Articles/f769698f-fed6-43eb-8e61-d7baaf713819/Login%20and%20Registration%20Form%20in%20ASP%20Net%20MVC#.VZqfdfkSyKs\
\
http://www.mindstick.com/Blog/648/Login%20Form%20in%20Asp%20Net%20MVC%204#.VZqfgfkSyKs\
\
http://www.mindstick.com/blog/647/Creating%20a%20simple%20data%20entry%20application%20using%20Asp%20Net%20MVC%204#.VZqfePkSyKs\
\
http://www.mindstick.com/blog/795/Post%20data%20With%20Ajax%20and%20ASP%20NET%20MVC#.VZqfTPkSyKs\


---

Original Source: https://www.mindstick.com/forum/23323/how-to-pass-data-from-action-to-ajax-success-function-in-mvc4

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
