0

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.

0

1 Answer 1

1

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; }
}
Sign up to request clarification or add additional context in comments.

5 Comments

it works ! but I don't understand. I used to use WebMethod. The way I used worked fine on WebMethod. Besides, how can we post 2 objects then ?
If you wanted to post two objects, you would create a composite object (OrderOptionWrapper) that contained two properties, OrderOption and your new object property. Hopefully that helps clarify it for you :)
That is kinda a hacky way. But thanks for the help !
Not at all. If you have multiple objects, you'd want to denote their sibling relationship, which is what that does. If they AREN'T siblings, they shouldn't be posted as part of the same call since that breaks the REST pattern. And my pleasure :)
huh, this explanation does make sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.