Post FromBody Always Null

后端 未结 6 1437
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 03:01

I\'ve got a new API that I\'m building with ASP.NET Core, and I can\'t get any data POST\'ed to an endpoint.

Here\'s what the endpoint looks like:

         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-29 03:18

    Another reason for the model binding to fail (always null) is if the data type for a property doesn't match. For example here is a simple model:

    public class MyService {
        public string JobId { get; set; }
        public int ServiceType {get; set;}
    }
    

    And here is some json that doesn't match:

    {"JobId":1, "ServiceType":1}
    

    I got caught with this when I was retrieving the JobId using jquery's .data function, it was automatically converting it to an int. Fixed it by using .attr function instead.

提交回复
热议问题