0

I have a $scope.

For simplicity:

    var cars = {
        "car": [
            { "name": "wtf1" },
            { "name": "wtf2" },
            { "name": "wtf3" }
        ]
    }

I am then sending this information to:

    $scope.Update = facUpdate.doUpdate({
        stest: cars
    },{}, function success(data, status, headers, config) {
            console.log(data);
    }, function err(data, status, headers, config) {
        console.log(data);
        //errorhandler(data.status, $location)
    });

I have two models in my C# API.

public class CarModel
{
    public IList<Car> cars { get; set; }
}


public class Car
{
    public string name { get; set; }
}

And Finally: This is where i am trying to get data...

[HttpPost]
public HttpResponseMessage Post(CarModel stest)
{

    string json = JsonConvert.SerializeObject(queryString);

    StringContent sc = new StringContent(json);
    sc.Headers.ContentType = new MediaTypeHeaderValue("application/json");

    HttpResponseMessage resp = new HttpResponseMessage();
    resp.Content = sc;

    return resp;
}

Header Info

POST /api/UpdateTest?stest=Saab&stest=Volvo&stest=BMW HTTP/1.1
Host: localhost:53839
Accept: application/json, text/plain, */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Content-Type: application/json;charset=UTF-8
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/35.0.1916.114 Safari/537.36

In a nutshell i have a $scope on my angular js that has changed and i want to pass that object back via post to my api.

If i use a simple string example it works, but when i get to complex objects it is not working.

Anyone know why?

Let me Know!


Factory Info

myApp.factory('facUpdate', function ($resource) {
    return $resource('/api/UpdateSettings/:eid', {}, {        
        doUpdate: { method: 'POST', isArray: false}
    })
});

ANSWER At least this worked for me...

on the Web API Post(string stest)

then do

ChartSettingsModel yourobject= JsonConvert.DeserializeObject(stest);

ADL
  • 2,707
  • 1
  • 16
  • 23
  • 1
    What happens if you rename the `car` array to `cars` in your JSON? As you have it, the JSON doesn't match your `CarModel`. I can't say if that's the entire issue since you didn't really specify in what way its not working. – Neil Smith May 28 '14 at 22:16
  • Let me try that.. however it is basically null. That did not work either. – ADL May 28 '14 at 22:24
  • Basically null? What is that? It's either null or it isn't. If your JSON's object names don't match up with the ViewModel's properties, ASP.NET's model binding wont... well, bind to the model correctly. :P Let me know if that works, and if not, edit your question about what specifically is not working. – Neil Smith May 28 '14 at 22:27
  • When i am debugging. public HttpResponseMessage Post(CarModel stest) stest (carmodel) is null. When i check my requesturi data looks like this: 'http://localhost:53839/api/ChartSettings?stest={"cars":[{"name":"wtf1"},{"name":"wtf2"},{"name":"wtf3"}]}' – ADL May 28 '14 at 22:28
  • Let me see your js that posts the data. – Neil Smith May 28 '14 at 22:40
  • Added the factory there.. However its the $scope.Update is where i am actually doing the sending. – ADL May 28 '14 at 22:47
  • I haven't been working with Angular long and I've never used ngResource; I've only used $http - just a preface. But to me it looks like you're using ngResource incorrectly. Its appending the post data to the url which isn't right. Check out the link. It appears to be the same issue you're having. http://stackoverflow.com/questions/20458140/ngresource-appends-post-parameters-to-url – Neil Smith May 28 '14 at 23:03
  • Figured it out.. not fully but found something that works Merci! – ADL May 28 '14 at 23:34

1 Answers1

0

ChartSettingsModel myclass= JsonConvert.DeserializeObject(stest);

ADL
  • 2,707
  • 1
  • 16
  • 23