0

I am building an application using ASP.NET Core MVC and I have the requirement of having dynamic Data annotations for some fields. For instance, the data annotation [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long", MinimumLength = 3)] I am using on an input type text, in some cases when the page is open must be 3 in other cases it must be 6.

How can I achieve this dynamically?

6
  • Does the validation need to be handled with real time validation, or can it be a response from the server? Also, where are the dynamic values coming from? Commented Sep 8, 2017 at 3:57
  • @WillRay Yes, it has to be real time validation and the values are coming from the database. I intend to have editable fields where I can set the maximum and minimum string length and save it in to the database Commented Sep 8, 2017 at 10:09
  • Your easiest solution might be to use the RemoteAttribute, and use the injection for controllers to check the length. Commented Sep 10, 2017 at 23:02
  • @WillRay thanks! I haven´t tried it yet but this seems like a solution. I´ll write an answer once I try it and works Commented Sep 18, 2017 at 18:05
  • I can create a sample demonstrating it's implementation in an answer, if that would be helpful for you? Commented Sep 20, 2017 at 1:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.