0

I have created an API in ASP.NET Core 2.2 framework as per below sample

[HttpPost]
[Authorize]
public IActionResult AddInvoiceRest([FromBody]AddInvoiceRetailRest[] InvoiceDetail)
{
}

here AddInvoiceRetailRest is class object. I want to pass multiple objects as array.

I am doing testing with postman. I have passed this array from raw body as per below sample.

{
  "InvoiceDetail": [
    {
      "InvoiceMst": [
        {
          "InvoiceNo": 0,
          "CustId": 0,
          "SubTotal": 93,
          "TaxAmount": 13
        }
      ]
    }
  ]
}

problem is here in api i received blank array without adding [Frombody] and when i add [FromBody] , Api doesn't call and shows error like below

{
    "InvoiceDetail": [
        "The input was not valid."
    ]
}

definition of class is

public class AddInvoiceRetailRest   
    {
        public AddInvoiceMst[] InvoiceMst { get; set; }
        public AddInvoiceItemDetail[] InvoiceItemDetail { get; set; }
        public AddInvoicePaymentDetail[] InvoicePaymentDetail { get; set; }
        [Optional]
        public AddInvoiceItemLogDetail[] InvoiceItemLog { get; set; }
        [Optional]
        public string BillDetail { get; set; }
        [Optional]
        public PumpCartObject[] InvoicePumpCart { get; set; }
        [Optional]
        public InvoiceFeeOrDeposite[] InvoiceFeeOrDeposite { get; set; }
    }

Here in the question, I just put a sample of the request, not all keys. Can someone please let me know what I'm doing wrong?

8
  • What is the error? Commented Nov 12, 2019 at 12:36
  • @mororo received blank array. doesn't receive data, Commented Nov 12, 2019 at 12:38
  • You don't need {""InvoiceDetail"": in start and } in end Commented Nov 12, 2019 at 12:38
  • Can you please post the AddInvoiceRetailRest class definition? I see double square brackets and was wondering if any collection is present in class definition Commented Nov 12, 2019 at 12:40
  • @mororo yeah. actually here in question i just have posted sample. let me edit question and post full defination Commented Nov 12, 2019 at 12:41

2 Answers 2

2

Your issue is that you are expecting array in your API but you are passing a single object json and not an array.

Try this:

[
    {
        "InvoiceMst":
        [
            {
                "InvoiceNo": 0,
                "CustId": 0,
                "SubTotal": 93,
                "TaxAmount": 13
            }
        ]
    }
]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. it is working. basically my main problem was i was passing InvoiceDetail key name with { } bracket. it was not required, Thanks a lot brother, i really appreciate it.
@AbhijitPandya, It wasn't just the keyword. Your json starts with { which means that you are trying to pass a single object rather than an array. It needs to start with [ which is an array.
2

The posted data does not match the expectation of the action.

One option is to create a model that matches the expected data

public class InvoiceDetailModel {
    public AddInvoiceRetailRest[] InvoiceDetail { get; set; }
}

And bind to that in the action

[HttpPost]
[Authorize]
public IActionResult AddInvoiceRest([FromBody]InvoiceDetailModel model) {
    if(ModelState.IsValid) {
        AddInvoiceRetailRest[] invoiceDetail = model.InvoiceDetail;

        //...
    }

    return BadRequest(ModelState);
}

Comments

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.