I'm new to AngularJS, trying to pass the ID from the url to MVC controller JSON method and then return the JSON to the angular scope.
Here is my MVC Controller:
public ActionResult Client(int clientid)
{
return View();
}
public JsonResult GetClient(string clientid)
{
ClientDao clientDao = new ClientDao();
Client client = clientDao.getClientById(int.Parse(clientid));
return new JsonResult { Data = client, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
And here is my Angular .js file:
var app = angular.module('myapp', ['ngRoute']);
app.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when('/Home/Client/:clientid', {
controller: 'ClientCtrl'
})
.otherwise({
redirectTo: "/Home/Index"
})
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
});
app.controller("ClientCtrl", function ($scope, $http, $routeParams) {
// alert($routeParams.clientid);
$http({
url: "/Home/GetClient/",
params: {clientid: $routeParams.clientid},
method: "get"
})
.then (function (response) {
$scope.client = response.data;
})
});
The problem is that the parameter clientid
in the $routeParams
is always undefined. How should I fix this ?
Update: Here's how my link looks like: http://something.com/Home/Client/20