The scenario I have seems pretty common but I did not found good solution so far. So there's ASP.NET-MVC application with MSSQL database sitting in back-end. The model includes class A with string field Description that needs to be limited to 100 characters. It's declared as follows:
[StringLength(100)]
public virtual string Description { get; set; }
The corresponding column in the database is nvarchar with column length = 100. In the UI, Description field is represented by textarea with maxlength = 100.
Now the issue comes when there're line breaks in the textarea. They are interpreted as 1 character on client side but on server side they are represented by Environment.NewLine which is 2 characters (\r\n). So it exceeds the database column length (actually server side validation fails before the whole back-end thing but let's omit the validation for simplicity).
The solutions I found so far are:
I guess there should be something else besides those three.
This is an old and known issue with MVC (I am not sure if it was solved) but different browsers treat line break differently. My suggestion is to custom model binder like what you find here jQuery validate textarea maxlength bug
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With