简体   繁体   中英

How to display data annotation error, the way it's property are decorated?

I m using WEB API (.net 6) for my project. For validation I m using data annotation. it does not display error, the way I decorated the property

My property

public string CompanyName { get; set; }

public string CompanyCode { get; set; }

public string Address { get; set; }

public string ZipCode { get; set; }

public string MobileNo { get; set; }

All the property I validated with data annotation. below image

在此处输入图像描述

When my API response back, errors display like this

在此处输入图像描述

But I want to display all the errors in the way that I decorated my property. first it will provide error about company Name, then company code then address and go on.

Is there any way to do this.

you have two Error Messages, remove one that is not required or replace the error message at top of the property. [Required Error Message("Error Message here...")] public string CompanyName { get; set; }

It is working in order, so first required will be validated and as soon as it throws an error that is being returned. if you enter something in company name property then it will try to validate the regex and will share the appropriate error message specified for regex validation.

You have to use custom validation to display such kind of messages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM