0

Routing

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

my link contains

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

my Angular controller

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

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

I have also used $routeParams instead $stateParams.

My MVC Controller

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

In Asp.net MVC Controller Id is always Null,

AKASH
  • 416
  • 1
  • 7
  • 19

2 Answers2

1

I think you should change your link to:

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

Presuming it sits within the scope of studentEditCtrl.

And use $routeParams

Starscream1984
  • 3,072
  • 1
  • 19
  • 27
  • Edit what if i put 2. it's also not working. ng controller alert is firing after Mvc Controller has returned view. – AKASH Aug 05 '15 at 12:36
  • This is confusing me a little - it seems more like you are trying to mix MVC and client-side routing, which is not really going to work as you describe. You should have the routing done client-side, and then when you need to get the student record, you should make a Web API controller call via http. – Starscream1984 Aug 05 '15 at 14:26
  • Is there any way to define dynamic parameter in templateUrl?? – AKASH Aug 05 '15 at 14:35
  • I don't think so - like I said, I think you're approaching this all wrong. Are you trying to build a SPA or an MVC app? – Starscream1984 Aug 05 '15 at 14:40
0

Thanks #Starscream1984
I got my answer from here.

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

        })
Community
  • 1
  • 1
AKASH
  • 416
  • 1
  • 7
  • 19