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 $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);

share|improve this question
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 at 22:16
    
Let me try that.. however it is basically null. That did not work either. –  user1040131 May 28 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 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: 'localhost:53839/api/ChartSettings?stest={"cars":[{"name":"wtf1"},{"name"‌​:"wtf2"},{"name":"wtf3"}]}' –  user1040131 May 28 at 22:28
    
Let me see your js that posts the data. –  Neil Smith May 28 at 22:40

1 Answer 1

ChartSettingsModel myclass= JsonConvert.DeserializeObject(stest);

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.