Click here to Skip to main content
15,125,547 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do I redirect to Home page after I login.. Below I added my Login controller and View and related JavaScript. Please help me soon.
This is my Controller:

C#
[HttpPost]
        public JsonResult Login(User user)
        {
            Response Res = new Response();
            //try
            //{
            string data = "";
                List<user> userlist = new List<user>();
                userlist = us.getByname(user.UserName);
                User U = new User();
                if (userlist.Count > 0)
                {
                    U = userlist.SingleOrDefault(a => a.Password == user.Password);
                    if (U != null)
                    {
                        data = "Login Success";
                    }
                    else
                    {
                        data = "Login faliur";
                    }
                }

                else
                {
                data = "Login faliur";
                }
            return Json(data,JsonRequestBehavior.AllowGet);
        }


and, this is my View along with the necessary JavaScript:

HTML
div class="container"&gt;
        <div ng-controller="lnCtrl">
            <h1>Welcome</h1>{{Message}}
            <form class="form">
                <input type="text" ng-model="user.UserName" placeholder="Username">
                <input type="password" ng-model="user.Password" placeholder="Password">
                <button type="submit" ng-click="Login(user)" id="login-button">Login</button>
            </form>
        </div>
    

var AlApp = angular.module("AlApp", []);
    AlApp.controller("lnCtrl", function ($scope, $http, $log) {
        var Us = $scope.user;
        $scope.Message = "Login";
        $scope.Login = function (Us) {
            //var Us = $scope.Us;
            //alert(angular.toJson(Us));
            $http({
                method: "Post",
                url: "/login/Login",
                data: JSON.stringify(Us),

                //data: Us


            }).success(function (data) {
                //alert(data);
                $scope.Message = data;

            }).error(function (err) {

                $scope.Message = err.Message;
            })
        };
    });
Posted
Updated 1-Dec-15 3:35am
v2
Comments
F-ES Sitecore 1-Dec-15 9:26am
   
Just use a normal form.

If you are using ajax call, you have to return the url in your "data" for example like data.url and use:

// similar behavior as an HTTP redirect
window.location.replace(data.url);

// similar behavior as clicking on a link
window.location.href = data.url;
   
Hi,

Since you are working with Ajax form submission, you can not redirect to another view from controller action. So you need to do this on client side after ajax get success.
So put window.location=''; into the java script code as:

JavaScript
}).success(function (data) {
               //alert(data);
               $scope.Message = data;
               window.location='/home';
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900