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,

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

<a class="btn btn-primary" ng-href="#/student/2">Edit</a> what if i put 2. it's also not working. ng controller alert is firing after Mvc Controller has returned view.
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.
Is there any way to define dynamic parameter in templateUrl??
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?
0

Thanks #Starscream1984
I got my answer from here.

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

        })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.