Bohn
Bohn

Reputation: 26919

Passing more than one JSON object to the Post method

In TypeScript I have something like this:

$.post(this.serviceUrl + '/GetMyCodes', JSON.stringify(item.MyCodeList.trim()), function (CodeData: Something.SomethingElse) {

and my POST method is something like this:

    [HttpPost]
    public JsonResult GetMyCodes([JsonBody] string myCode)

My problem is how can I need to pass one more value to this service. How can I do that?

Upvotes: 0

Views: 257

Answers (1)

Marcus Höglund
Marcus Höglund

Reputation: 16801

You could create a model which holds all values you want to post

public class postModel
{
    public string myCode { get; set; }
    public string myDescription { get; set; }
}

Define the controller to read the model from the body

[HttpPost]
public JsonResult GetMyCodes([FromBody] postModel model)

Then create an object in your client that matches the model structure

var obj = { myCode: 'myCode', myDescription: 'myDescription' };
$.post(this.serviceUrl + '/GetMyCodes', JSON.stringify(obj);

Upvotes: 1

Related Questions