0

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

Community
  • 1
  • 1
Alfero Chingono
  • 2,663
  • 3
  • 33
  • 54
  • http://stackoverflow.com/questions/6801656/maxlength-attribute-not-generating-client-side-validation-attributes – Matt Bodily Mar 03 '14 at 14:43
  • No, not the same problem. This issue can be solved in a similar fashion, but I'm concerned about doing things the "right/recommended way". – Alfero Chingono Mar 03 '14 at 14:44

1 Answers1

0

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.

Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
  • Thanks, I do know that, but I need it to work without specifying an actual max length. You see, EF ensures that the column is created as NTEXT (I believe) if I don't specify a max length value, so I need that. – Alfero Chingono Mar 03 '14 at 14:50
  • Do you use your EF entity as view model as well? – Henk Mollema Mar 03 '14 at 14:53
  • Nope, but I scaffold my models from the EF entities. That way, they 'inherit' the attributes from the EF entities. I can change the scaffolding logic to add the max length value if it is missing, but I'd rather not do that if I don't have to. – Alfero Chingono Mar 03 '14 at 14:55
  • You shouldn't do that in my opinion, to avoid such issues. – Henk Mollema Mar 03 '14 at 14:58
  • Hmmm, I'm not sure I agree. Unless there is absolutely no other better way of handling this issue. – Alfero Chingono Mar 03 '14 at 15:02
  • They serve different purposes, so why would you inherit from each other or create dependencies to each other. – Henk Mollema Mar 03 '14 at 15:06