重定向到行动

时间:2017-02-01 17:45:25

标签: angularjs asp.net-mvc

在以下问题中需要帮助。我的问题是从angularjs获取凭据到Action MVC它工作正常但View没有重定向到其他ActionResult。为什么?需要解决方案 这是我的观点:

<body ng-app="LoginModule">
<div id="body" ng-controller="LoginController">
    <div>``
        <div>
            <label id="labels">User Name:</label> <input type="text" ng-model="User.UserName" />
            <br />
            <label id="labels">Password:&nbsp;</label> <input type="password" ng-model="User.UserPassword" />
            <br />
            <input id="buttons" class="btn btn-primary" value="Login" type="button" ng-click="Login()" />
        </div>
    </div>
</div>

这是我的js:

var loginModuel = angular.module("LoginModule", [])
loginModuel.controller("LoginController", function ($scope, $http) {
    alert("LoginController");
    $scope.Login = function()
    {
        alert("Login Function");
        var result = $http({ method: 'POST', url: '/Home/Login', params: { user: $scope.User } });
        result.success((function() {
            window.location.href = '/Doctor/DoctorPanel';
        }));
        alert($scope.User.UserName + "-" + $scope.User.UserPassword);
    }
});

和MVC行动是:

[HttpPost]
public ActionResult Login()
{
    JObject json = JObject.Parse(Request["user"]);
    string userName = (string)json["UserName"];
    string userPass = (string)json["UserPassword"];
    User u = New User();
    u.User_Name = "Test";
    u.User_Password ="ABCD";
    if (u.Users_Name == userName && u.Users_Password == userPass)
    {
        return View("WelcomUser","Home");
    }
    return View("Index","Home");          
}

2 个答案:

答案 0 :(得分:0)

您使用的是表单身份验证吗?在您的登录中,您没有设置身份验证cookie或其他任何内容,因此没有建立凭据会导致任何重定向只返回登录。

另外你根本没有使用控制器的返回值(它返回HTML而不是重定向),而是将窗口位置设置为/ Doctor / DoctorPanel,我认为它不允许匿名访问,因此您将被重定向回登录。

答案 1 :(得分:0)

在“返回视图”(“WelcomUser”,“Home”)中,您的视图名称是否正确;“ ,当“返回视图”命中时,您的开发人员工具控制台是否会给您任何消息?我最好猜测你的Angular脚本无法解析视图位置因此失败,然后不允许加载其他视图。