Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Data validation #3990


User avatar
0
pankajmathur created

Hi,

I have added a max length attribute to my DTO. However when I create an object of DTO and assign a string more than the permissible length, it does not give me error and the error is coming only when the object is getting committed in the database.

   [Required]
   [MaxLength(100)]
    public string Description { get; set; }

Am I doing something wrong or it is a default behavior.


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @pankajmathur,

    What you did is correct actually, see <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Validating-Data-Transfer-Objects#DocUsingAnnotations">https://aspnetboilerplate.com/Pages/Doc ... nnotations</a>.

    Can you share your dto class ?

  • User Avatar
    0
    pankajmathur created

    [AutoMapTo(typeof(TaxCode))] public class CreateOrUpdateTaxCodeDto: Entity { [Required] [MaxLength(20)] public string EntityId { get; set; }

        [Required]
        [MaxLength(100)]
        public string Description { get; set; }
    
        [Required]
        public decimal Rate { get; set; }
        
    }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    It should be defined like this:

    public class CreateOrUpdateTaxCodeDto: EntityDto
    

    Can you try it like that and see if it works ?