locked
ErrorMessage for decimal in entity-framework-core RRS feed

  • Question

  • User-323149085 posted

    Hello im trying to customize error Message for decimal but it look like it is not work & im just getting the default error MSG , it's probably out of the box gut I just cant find it .

    Else > how can I overwrite  the default ? 

    Code example  :

    using System;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;
        using Market.Models;
    
        namespace Market.ViewModels
        {
            public class QuettaCatViewModel
        [Required(ErrorMessage = "Please fill Amount ")] //This Work fine
    
                public int Qentity { get; set; }
         [Required(ErrorMessage = "Value cant empty")] // Not Fine Getting the default Msg 
                //[RegularExpression(@"^\d+.\d{0,2}$", ErrorMessage = "Value cant be empty")] //// Not Fine Getting the default Msg + The RegularExpression error Msg  
                public decimal Bedget { get; set; }

    thanks

    Tuesday, May 21, 2019 2:19 PM

Answers

  • User-323149085 posted

    @Ackerly thanks it was  my bed, forgot _ValidationScriptsPartial in view  (there is No need to change decimal to decimal ? )

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 22, 2019 7:49 AM

All replies

  • User-893317190 posted

    Hi john_mm,

    decimal is a simple  type which couldn't be null, so the required attribute doesn't work.

    If you want it to be null, please change decimal to decimal?, which will allow null value, then your Required will work when the value of Bedget is null.

    Below is my test code.

    My model

     public class Movie
        {
            public int Id { get; set; }
            [StringLength(60,MinimumLength =3)]
            [Required(ErrorMessage ="need title")]
            public string Title { get; set; }
            [DataType(DataType.Date)]
            public DateTime? ReleaseDate { get; set; }
            public string Genre { get; set; }
            //[Range(1,100)]
            [DataType(DataType.Currency)]
            [Required(ErrorMessage = "Value cant empty")]
            public decimal? Price { get; set; }
        }

     cshtml.

    <div class="col-md-4">
            <form asp-action="Create">
                 
                      @* other fields are ommited  *@
                <div class="form-group">
                    <label asp-for="Price" class="control-label"></label>
                    <input asp-for="Price" class="form-control" />
                    <span asp-validation-for="Price" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </form>

    The result.

    Best regards,

    Ackerly Xu

    Wednesday, May 22, 2019 3:24 AM
  • User-323149085 posted

    @Ackerly thanks it was  my bed, forgot _ValidationScriptsPartial in view  (there is No need to change decimal to decimal ? )

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 22, 2019 7:49 AM