I am doing Ajax requests to server in following way:
App Setup:
var appRoot = angular.module('demoApp', ['ngRoute', 'ngResource']);
appRoot.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/', { templateUrl: '/ngPartials/_Login.html', controller: 'LoginCtrl' })
.otherwise({ redirectTo: '/ngPartials/_Login.html', controller: 'LoginCtrl' });
}
]);
Factory:
appRoot.factory('LoginResource', function ($http) {
var loginResource = {
response:{},
get: function (param) {
$http.get('/Login/Login', param).
success(function (data, status, headers, config) {
this.response = data;
}).
error(function (data, status, headers, config) {
alert('error');
});
}
}
return loginResource;
});
Controller:
appRoot.controller('LoginCtrl', function ($scope, LoginResource) {
//Make a get request on login resource
$scope.User = LoginResource.get(
{
Role: val,
Email_Id: $scope.Email,
Pwd: $scope.Password
}, function (response) {
//do someting in callback function
});
});
But I don't know what wrong I am doing in above code, the parameters (Role,Email_Id,Pwd) are not being binded to my server Action method when I do get request to it. The method is hit properly but the model values are not being binded.
server side model:
public class LoginModel
{
public string Role { get; set; }
public string Email_Id { get; set; }
public string Pwd { get; set; }
public bool IsEmailValidation { get; set; }
}
Action method:
[HttpGet, ActionName("Login")]
public JsonResult Login(LoginModel oLogin)
{
...
}
When I call above action method from angular, the method is being hit, but all the properties in the object oLogin are null. What going wrong with my above code?
EDIT:
Previously instead of $http, I was using $resource as follows and everything was working exactly fine.
appRoot.factory('LoginResource', function ($resource) {
return $resource('/Login/Login');
});