Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a simple WebApi that I'd like to connect to an angularjs app

The api looks something like this

public HttpResponseMessage GetSokAvtal(SokAvtalAdvanced sokAvtalAdvanced)
{
    if (sokAvtalAdvanced != null && sokAvtalAdvanced.IsValid())
    {
        try
        {
            var avtal = db.spSokAvtalAdvanced(
                limit: sokAvtalAdvanced.Limit,
                offset: sokAvtalAdvanced.Offset);

            return Request.CreateResponse(HttpStatusCode.OK, avtal);
        }
        catch (Exception ex)
        {
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Ett fel har inträffat");
        }
    }

    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Felaktiga parametrar");
}

The SokAvtalAdvanced looks like this

public class SokAvtalAdvanced
{
    #region Properties

    private int _limit;
    public int Limit
    {
        get { return _limit > 0 && _limit < 50 ? _limit : 10; }
        set { _limit = value;  }
    }

    private int _offset;
    public int Offset
    {
        get { return _offset > 0 ? _offset : 0; }
        set { _offset = value; }
    }

    #endregion

    #region Methods

    /// <summary>
    /// Returns a valid modelstate
    /// </summary>
    /// <returns></returns>
    public bool IsValid()
    {
        return true;
    }

    #endregion

}

To map this to angular I've done this so far without any luck

app.controller("SokAvtalController", ['$scope', 'SokApi', function ($scope, SokApi) {
    $scope.avtal = SokApi.query({ sokAvtalAdvanced: {
            "Limit": 10,
            "Offset": 0
    }});

    console.log($scope.avtal);

    $scope.SelectGroup = function (avtal) {

    };
}]);

angular.module('app.avtalsportalen', ['ngResource'])
    .factory('SokApi', ['$resource', function ($resource) {
        return $resource('/api/SokAvtal/:sokAvtalAdvanced');
    }]);

Any idea of what's wrong with my call? The SokAvtalAdvanced sokAvtalAdvanced is null in WebApi every call

Thanks

share|improve this question
    
Have you tried just using SokApi.query({ "Limit": 10, "Offset": 0 }); –  Wayne Ellery Jan 26 at 11:54
    
@WayneEllery Yea, same result –  Eric Herlitz Jan 26 at 11:58

1 Answer 1

Since this is a GET request you need to indicate the model binder to read from the url by using the [FromUri] attribute:

public HttpResponseMessage GetSokAvtal([FromUri] SokAvtalAdvanced sokAvtalAdvanced)
{
    ...
}

Also on the client you should use:

$scope.avtal = SokApi.query({
    "Limit": 10,
    "Offset": 0
});
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.