mordeby
mordeby

Reputation: 77

JSON Data Could Not Read

I have a Post Method as GetProductList, I post some inputs and the method filtered the data and returns a list. I try this method on Postman with JSON. I post the inputs but they come null. I add the debug ss. Why this happen?

Method:

        [HttpPost]
        [Route("GetProductList")]
        public async Task<IActionResult> GetProductList(int id, string? productName, string? companyName, int productInfo, int operationType, string? startDate, string? endDate)
        {
            var result = _productsService.GetProductList(id,productName, companyName, productInfo, operationType, startDate, endDate);

        return Ok(result);
    }

JSON:

{"id":152,"productName":null,"companyName":"XCompany","productInfo":5,"startDate":"2021-08-03","endDate":"2021-08-03","operationType":1}

ScreenShot from debugging

enter image description here

Upvotes: 0

Views: 73

Answers (1)

Serge
Serge

Reputation: 43959

It is always a good idea to create a viewmodel, especially when you use post

public class ProductViewModel
{

public int Id {get; set;}
public string ProductName {get; set;}
public string CompanyName {get; set;}
.....

}

and when you use a Postman it is always good to try to add [FromBody]

[HttpPost]
 [Route("GetProductList")]
 public async Task<IActionResult> GetProductList([FromBody] ProductViewModel viewModel)
 {
 var result = _productsService.GetProductList(viewModel.Id,viewModel.ProductName, ....);

        return Ok(result);
    }

you can remove [FromBody] after testing, if it is not working in net

Upvotes: 2

Related Questions