Vadzim Savenok
Vadzim Savenok

Reputation: 940

C# extend MVC maxlengthattribute

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!

Upvotes: 1

Views: 304

Answers (1)

riteshmeher
riteshmeher

Reputation: 874

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

Upvotes: 1

Related Questions