Question

If I add a MaxLengthAttribute to a property like this:

[MaxLength]
[DataType(DataType.MultilineText)]
public string Notes { get; set; }

The markup is rendered like this:

<textarea class="form-control" data-bind="value: Notes" data-val="true" data-val-maxlength="The field Notes must be a string or array type with a maximum length of '-1'." data-val-maxlength-max="-1" id="Notes" name="Notes"></textarea>

and the validation result looks something like this:

enter image description here

This is obviously not the intended result. The text area should allow A LOT more characters than '-1'.

I can think of multiple ways of addressing this (e.g. removing the attribute via jQuery, manually updating the rule with javascript, etc).

What is the most elegant way of addressing this issue? Is this a bug with MVC/Validator

Was it helpful?

Solution

You are not specifying the desired maximum length of the string, what do you expect? This overload of the MaxLength attributes takes an integer as argument which specifies the actual maximum allowed length of the string. When you use the parameter-less constructor it will use the maximum length allowed by the database, not sure why jQuery Validation chose to implement this as -1 though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top