Stop ASP.NET Core 3.1 [FromForm] Converting non-zero decimal to 0

GimpFlamingo :

When I POST to the following, it is converting the non-zero JobNumber to 0 instead of the sent value.

 [HttpPost("{testName}")]
        public async Task<ActionResult<FirstPieceStamps>> PostFirstPieceStamps(string testName, [FromForm] FirstPieceStamps firstPieceStamps)

Here is the FirstPieceStamps model

public class FirstPieceStamps
    {
        [Column(TypeName = "decimal(10,3)")]
        public decimal JobNumber { get; set; }

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int StampID { get; set; }

        [Required]
        public int TestID { get; set; }

        [Required]
        public int UserTypeID { get; set; }

        [Required]
        public bool Stamped { get; set; } = false;
#nullable enable
        public string? FirstName { get; set; }

        public string? LastName { get; set; }
#nullable disable
        public FirstPieceJobs FirstPieceJobs { get; set; }

        public FirstPieceTests FirstPieceTests { get; set; }

        public FirstPieceUserTypes FirstPieceUserType { get; set; }

    }

The request I'm trying to make from Postman to test it.

https://localhost:5001/api/FirstPieceStamps/Layflat?JobNumber=266019.001&Stamped=true&FirstName=John&LastName=Doe

The JobNumber value should be converted to a decimal number, but is instead being converted to just 0.

Bryan Lewis :

Check your use of FromForm. If you are really sending this POST as you have above, then the params are on the query string and you should be using [FromQuery] instead. I created a fresh Core 3.1 API project with your model and this controller method and it only worked correctly when I changed FromForm to FromQuery. If you want FromForm to work, then you need to remove them from the query string and use "form-data" option in Postman. With FromQuery, the full JobNumber with decimals came through.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=390931&siteId=1