Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This is my Url which is using asp.net MVC routing

http://localhost:23293/Exam?id=1

i want to access id parameter in angular js controller

aoeapp.controller('ExamController', function ($scope, examservice, $routeParams) {
$scope.exams = [];
$scope.id = $routeParams.id;
alert($scope.id);
$scope.getAllexams = function (id) {

    examservice.getExamMaster(id).success(function (data) {
        $scope.exams = data;
    }).error(function (data, status, Xhr) {

        alert(Xhr);
    });
};
$scope.getAllexams(id);
});

so here $scope.id showing undefined

My mvc routing is just default routing

Edit The angular routes

aoeapp.config(function ($routeProvider) { 
    $routeProvider.when('/', { 
        controller: 'HomeController', 
        templateUrl: 'Pages/Home.html' 
    }).when('/Exam/:id', { 
        controller: 'ExamController', 
        templateUrl: 'Pages/Exam.html' 
    }).when('/Test/:id', { 
        controller: 'TestController', 
        templateUrl: 'Pages/Test.html' 
    }).otherwise({ redirectTo: '/' }); 
});
share|improve this question
    
Angular doesn't care about your server side routing. What are your Angular routes? – Andy Gaskell Feb 13 '16 at 6:22
    
ok , But if i am using angular routes than it is conflicting with MVC routing and it is not working – Avinash Raikwar Feb 13 '16 at 6:35
    
You can use routing on both sides but your Angular application only cares about Angular routes. Add your Angular routes to this question. – Andy Gaskell Feb 13 '16 at 6:41
1  
aoeapp.config(function ($routeProvider) { $routeProvider .when('/', { controller: 'HomeController', templateUrl: 'Pages/Home.html' }) .when('/Exam/:id', { controller: 'ExamController', templateUrl: 'Pages/Exam.html' }) .when('/Test/:id', { controller: 'TestController', templateUrl: 'Pages/Test.html' }) .otherwise({ redirectTo: '/' }); }); – Avinash Raikwar Feb 13 '16 at 6:54
    
but problem is that angular routing is not working application accepting only mvc route ..... so i want to knw what to change in routemap – Avinash Raikwar Feb 13 '16 at 6:55

I fixed this problem with a different approach. I created a ViewBag variable to store the query string or the id, you can also use the model, and then pass it into an angular method. You can understand this more clearly by going through the code:

Razor View

<form name="ExamForm" ng-init="$scope.setQueryStringToScopeId(@ViewBag.Id)">
    ....
</form>

Angular Controller

aoeapp.controller('ExamController', function ($scope, examservice) {

    $scope.id = 0;
    $scope.setQueryStringToScopeId = function(id) {
        $scope.id = id;
    };
    ....
});

MVC Controller

public ActionResult Exam()
{
    ViewBag.Id = Request.QueryString["Id"];
    return View();
}

You can also use the query string directly in your Razor View. Let me know if this works for you.

share|improve this answer
    
if i do alert inside $scope.setQueryStringToScopeId = function(id) { $scope.id = id; alert( $scope.id ); }; works fine inside function but outside this function again showing 0 value – Avinash Raikwar Feb 13 '16 at 14:19
    
I will have to see your code but once $scope.setQueryStringToScopeId is executed you can alert($scope.id) and it will work... – Syed Farjad Zia Zaidi Feb 14 '16 at 7:54

I had a similar issue, where my Angular routes were routed to my MVC controllers. I fixed it using a catchall route:

Add this route to your App_Start/RouteConfig.cs:

routes.MapMvcAttributeRoutes();
routes.MapRoute(
    name: "Angular",
    url: "Exam/{*url}",
    defaults: new {controller = "Angular", action = "Index" }
);

With the following action in your Controllers/AngularController.cs file:

[Route("Exam/{*url}")]
public ActionResult Index()
{
    return View();
}

This should intercept all calls to the /Exam and redirect them to your Angular router. You may have to play a litle with the Route attribute name.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.