简体   繁体   中英

AngularJS (Post) C# Web API $Scope

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

ChartSettingsModel myclass = JsonConvert.DeserializeObject(stest);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM