1

I have already searched here to answer my question, and the closest I ever got was that post, but it still does not completely clarify the matter to me, so I will ask.

What I need is extending maxlengthattribute in the way that when I set it inside the C# file,

[MaxLength(50)]
[Display(Name = "Project Description")]
public string ProjectDescription { get; set; }

the attribute "maxlength" will be added inside the tag and will be <\stuff stuff maxlength = "50">

I have initially implemented it as writing html-helper to modify TextBoxFor, however, this is out of option since project is tightly intertwined with .js, and inserting renamed method will break a code, which will be a pain to fix.

The answer I referred above provides the closest explanation of what I need, but it looks like I will need to declare attributes (like inside ( ) in function), which I do not see there.

At this point I can either try modifying JS file on the server-side, or extending maxlengthattribute. Thus far, latter is preferable for me, thus I would like to ask how to properly extend it inside the c# file.

Thank you very much in advance!

Community
  • 1
  • 1
Vadzim Savenok
  • 930
  • 3
  • 14
  • 37
  • MIght be off, but try use [StringLength(50, ErrorMessage = "ProjectDescription cannot be longer than 50 characters.")] – aggaton Jun 08 '16 at 19:25
  • @aggaton I have tried that already, however, what I need is maxlength, and physically preventing any characters beyond limit. – Vadzim Savenok Jun 08 '16 at 19:28
  • @riteshmeher Can you please help with the step 3 from the solution by Randhir? I do not have such file in views/shared, so I am not sure where to place it. Thank you in advance! – Vadzim Savenok Jun 08 '16 at 21:49

1 Answers1

1

You can write a custom dataannotation provider. The max length attribute will get added to the control. Refer this link

Community
  • 1
  • 1
riteshmeher
  • 854
  • 8
  • 16