MVC控制器获取NULL异常

时间:2017-01-16 03:51:13

标签: asp.net angularjs asp.net-mvc asp.net-core asp.net-mvc-controller

我使用的是ASP.NET Core和AngularJS(不是AngularJS 2)。我按照这个链接AngularJS Tutorial for Login with MVC但我的mvc控制器(AuthenticateUser)获取空值。虽然我通过我的脚本获得了这个价值 JSON: 密码:"测试" 用户名:"测试"

来源: {"用户名":"测试""密码":"测试"}

虽然我追加了[FromBody],但控制器(AuthenticateUser)仍然是空值。

这是我的代码: Service.js



app.service("myAppService", function ($http) {

    this.UserLogin = function (User) {
        var response = $http({
            method: "post",
            url: "../Administrator/AuthenticateUser",
            data: JSON.stringify(User),
            dataType: "json"
        });
        return response;
    }

});




Controller.js



app.controller("MyAppController", function ($scope, MyAppService) {

    $scope.LoginCheck = function () {
        var User = {
            UserName: $scope.usrName,
            Password: $scope.usrPassword
        };

        var getData = MyAppService.UserLogin(User);
        getData.then(function (d) {
            alert(d.UserName);
            
        });
        debugger;
    }

});




Index.cshtml



<div class="container">
    <div id="loginbox" style="margin-top:50px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
        <div class="panel panel-info">
            <div class="panel-heading">
                <div class="panel-title">Sign In</div>
                <div style="float:right; font-size: 80%; position: relative; top:-10px"><a href="#">Forgot password?</a></div>
            </div>
            <div style="padding-top:30px" class="panel-body">
                <div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>

                <form id="loginform" class="form-horizontal" role="form">

                    <div style="margin-bottom: 25px" class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                        <input type="text" class="form-control" ng-model="usrName" placeholder="Username">
                    </div>

                    <div style="margin-bottom: 25px" class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                        <input type="password" class="form-control" ng-model="usrPassword" placeholder="Password">
                    </div>

                    <div style="margin-top:10px" class="form-group">
                        <!-- Button -->
                        <div class="col-sm-12 controls">
                            <button type="button" class="btn btn-success" ng-click="LoginCheck();">Login</button>
                        </div>
                    </div>

                </form>

            </div>
        </div>
    </div>

</div>
&#13;
&#13;
&#13;

AdministratorController.cs

&#13;
&#13;
public class AdministratorController : Controller
    {
        private TestDBOnlineContext _context;

        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult AuthenticateUser(UserAccessViewModel _users)
        //public string AuthenticateUser(UserAccessViewModel userAccess)
        {
            //string msg = string.Empty;

            //if (userAccess.UserName == null || userAccess.UserPassword == null)
            //    return "Please fill-out all entries";
            //else
            //{
                var users = _context.UserAccess.Where(w => w.Username.Equals(_users.UserName) &&
                                                   w.Userpassword.Equals(_users.UserPassword));
            //     if (users != null)
            //         return "Thanks for logging.";
            //     else
            //         return "Invalid username or password.";

            //}
            return Json(users);
        } 
    }
&#13;
&#13;
&#13;

希望你能帮助我。我已经困了很多天了。提前谢谢。

0 个答案:

没有答案