Home > AJAX, ASP.NET, MVC > ASP.NET MVC String Editor Template which handles the StringLength attribute

ASP.NET MVC String Editor Template which handles the StringLength attribute

Interestingly enough I was searching for a way for MVC to automatically handle the stringlength property. Seemed quite straightforward to implement, but somehow there is no available code out there to figure this out. Using the Html.EditorFor just creates a no frills textbox, which is rather irritating.

Logic walkthrough

Basically what the template does is to attempt to find the StringLength attribute from the ModelMetadata, and from there get the validation parameters.

Once it finds the appropriate parameter (in this case max), it will assign this to a string dictionary.

Finally all is wrapped up with a call to Html.Textbox


to use this ‘custom’ editor template, what you need to do will be

@Html.EditorFor(model => model.strcolumn)

or if you want to specify the width as well

@Html.EditorFor(model => model.strcolumn, new { width = “300px” })

NOTE: This works if you specify Text as the datatype, otherwise you will need to change the top to include the template name

Code Segment (Text.cshtml)

Note that the following code segment is in razor format, but you can easily change that to a webcontrol.

@* Specifies that this is for the string data type *@
@model String
//Create a dictionary to hold all the html attributes to be applied, this follows the requirement for the Html.Textbox last property which requires IDictionary<string, object>
var o = new System.Collections.Generic.Dictionary<string, object>();

//add in the default css class, notice that the @ is not required in this case
o.Add(“class”, “text-box single-line”);

//Attempt to find the validation attribute for maxlength
foreach (var r in ViewData.ModelMetadata.GetValidators(this.ViewContext.Controller.ControllerContext))
//What we are looking for is a validator of type StringLengthAttributeAdapter
if (r is StringLengthAttributeAdapter)
var attr = (StringLengthAttributeAdapter)r;

var ValidationParameters = attr.GetClientValidationRules().First().ValidationParameters;

//Get the validation parameters, maxlength is a required parameter so it is definately there, but just check in case for some reason it is not there
if (ValidationParameters.ContainsKey(“max”))
//add the maxlength property to the dictionary
o.Add(“maxlength”, (int)ValidationParameters[“max”]);

//additional check to see if there is a width property specified
if (ViewData[“width”] != null)
o.Add(“style”, “width: ” + ViewData[“width”].ToString());

//Finally emit the html for the textbox
@Html.TextBox(“”, ViewData.TemplateInfo.FormattedModelValue, o);

Categories: AJAX, ASP.NET, MVC
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: