2

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');
});
6
  • Am I right in saying that you cannot pass a complex object with GET. Try using POST method and instead of params pass in a JSON object - data : param Commented Feb 21, 2015 at 13:43
  • I can't use POST instead of GET, 'cause I am using POST for other purpose. Also I tried sending JSON : var postdata = { data: param }; $http.get('/Login/Login', postdata) But it also not working. Commented Feb 21, 2015 at 13:49
  • @SaurabhLprocks have a look at this Commented Feb 21, 2015 at 13:57
  • @MohammadSepahvand - If I can't send complex object, then why previously I was able to send it using $resource? Commented Feb 21, 2015 at 14:05
  • Try adding the [FromUril] attribute to the LoginModel parameter. If that doesn't work, can you post your route config? asp.net/web-api/overview/formats-and-model-binding/… Commented Feb 21, 2015 at 15:18

1 Answer 1

1

In LoginResource, the second parameter to $http.get is the config, not the params...so change it to the following:

get: function (param) {
    $http.get('/Login/Login', { params: param }).
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.