在angularjs中使用$ routeProvider时,控制器中的参数为null

时间:2015-08-05 12:01:04

标签: angularjs asp.net-mvc-4

路由

 .when('/student/:Id', {
           templateUrl: 'student/index',
           controller: 'studentEditCtrl'
       })

我的链接包含

<a class="btn btn-primary" ng-href="#/student/@v.Id">Edit</a>

我的角色控制器

angular.module('newApp')
  .controller('studentEditCtrl', ['$scope', function ($scope, $stateParams) {
      $scope.isTabActive = true;

      $scope.Id = $stateParams.Id;
      alert("studentEditCtrl");
  }]);

我还使用了$ routeParams而不是$ stateParams。

我的MVC控制器

  public ActionResult Index(int? Id)
        {
            Student st = new Student();
            if (Id != null)
                st = sRepository.Students.FirstOrDefault(c => c.Id == Id);
            return View(st);
        }

在Asp.net中,MVC控制器ID始终为空,

2 个答案:

答案 0 :(得分:1)

我认为您应该将链接更改为:

<a class="btn btn-primary" ng-href="#/student/{{Id}}">Edit</a>

假设它位于studentEditCtrl范围内。

并使用$routeParams

答案 1 :(得分:0)

谢谢#Starscream1984
我得到了答案 here

 .when('/student/index/:Id', {
             controller: 'studentEditCtrl',
             templateUrl: function (urlattr) {
                 return '/student/index/' + urlattr.Id;
             },

        })