locked
Validation not working in Asp.Net Core RRS feed

  • Question

  • User1956580905 posted
    The Model: [Required(ErrorMessage = "Please provide the unit's price")] public decimal PURCHASE_PRICE { get; set; } [Required(ErrorMessage = "Please provide the unit's serial number")] [StringLength(55, ErrorMessage = "The unit's serial number cannot exceed {0} characters.")] public string SERIAL_NUMBER { get; set; } Issue: When both left blank and the form is submitted, the error message for SERIAL_NUMBER displays - "Provide provide the unit's serial number", but PURCHASE_PRICE displays "The value '' is invalid". What would be the potential cause of this or is it a bug and how can I fix it ?
    Wednesday, February 24, 2021 9:22 AM

All replies

  • User503812343 posted

    what value you are trying to use for Purchase_Price is it decimal?

    Wednesday, February 24, 2021 12:39 PM
  • User1686398519 posted

    Hi JoshuaMunstermann, 

    [Required(ErrorMessage = "Please provide the unit's price")]
    public decimal PURCHASE_PRICE { get; set; }
    [Required(ErrorMessage = "Please provide the unit's serial number")]
    [StringLength(55, ErrorMessage = "The unit's serial number cannot exceed {0} characters.")]
    public string SERIAL_NUMBER { get; set; }

    When both left blank and the form is submitted, the error message for SERIAL_NUMBER displays - "Provide provide the unit's serial number", but PURCHASE_PRICE displays "The value '' is invalid".

    According to the error message you provided, you need to check whether the value of PURCHASE_PRICE is a demical type of data.

    At the same time you need to check whether SERIAL_NUMBER has a value.

    You can click this link to learn Data Validation in MVC.

    Note: It is recommended that you use {;} to insert the code to make the code structure look clearer.

    Best Regards,

    YihuiSun

    Thursday, February 25, 2021 9:25 AM