maxisam
maxisam

Reputation: 22705

angularjs post complex data to WebApi 2

JS:

$http.post("/api/Checkout/SaveOrderOption", { OrderOption: OrderOption })

C#

[HttpPost]
public void SaveOrderOption(object OrderOption)
{
    _Logger.Trace(OrderOption.ToJSON());
}

It is really weird. If I just object, I can get the correct raw json string i post.

{"OrderOption":{"xxxx":"xxx","www":true,"yyy":true}}

but if I change the type to a specific type, it doesn't work.

The data of the object becomes default value instead of the value I post.

I tried [FromBody], it doesn't work either.

Upvotes: 0

Views: 593

Answers (1)

David L
David L

Reputation: 33815

By wrapping it in an object, you have an object within an object, which I'm guessing your type doesn't recognize. Just post the object itself, with an explicit route that expects it.

$http.post("/api/Checkout/SaveOrderOption", OrderOption)

[HttpPost]
[Route("Checkout/SaveOrderOption/{orderOption}")]
public void SaveOrderOption([FromBody]OrderOption orderOption)
{
    _Logger.Trace(orderOption.ToJSON());
}

public class OrderOption
{
    public string Xxxx { get; set; }
    public bool Www { get; set; }
    public bool Yyy { get; set; }
}

Upvotes: 1

Related Questions