Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation errors in asp.net 4.5

I have googled this extensively and couldn't find an answer, so I am posting this here hoping for help.

We have an asp.net 4.5 project with validation specified in the database objects classes. For example:

[Required(ErrorMessage = "Name is a required field.")]
public string Name { get; set; }

Is there any way at all that we can have this error message show up next to the field on the form page? The validation summary shows up just fine but we would really like to have them show up next to their respective fields without using the traditional, code-heavy asp.net validators (RequiredFieldValidator and others).

Thanks for the help.

EDIT: we are using webforms, not MVC

like image 780
user2085436 Avatar asked Oct 21 '22 17:10

user2085436


1 Answers

You can use the ModelErrorMessage control. For example, let's pretend you have a model with a FirstName property. You would add something like this:

<asp:ModelErrorMessage ID="FirstNameErrorMessage" ModelStateKey="FirstName" runat="server" />
like image 128
user2632796 Avatar answered Oct 28 '22 21:10

user2632796