Data Attribute Bug

Mar 28, 2011 at 10:26 PM

I'm not 100% certain but following Scott Hanselman's awesome presentation from November (PDC10 MVC3 Magic), I attempted to create the same blog application he was able to using the RTM and ran into an issue.  After creating the model for Post, he goes back and changes the data attribute to DataType.MultilineText and then rebuilds.  The View correctly displays a textarea, BUT, if you enter more than 128 characters, it fails.  I tried removing the attribute, changing the control manually in the view to a textarea, I was able to save as long as data value as I wanted.

Anyone else?

Mar 28, 2011 at 10:53 PM
Edited Mar 28, 2011 at 11:30 PM

What error did you get?

I did a test and the The DB type defaults to nvarchar(128) regardless of if it's a text area or not. I was able to increase this size using the maxlength attribute.

 

        [DataType(DataType.MultilineText)]
        [MaxLength(700)]
        public String Description { get; set; }
Mar 29, 2011 at 3:15 AM

"Validation failed for one or more entities.  See 'EntityValidationErrors' property for more details."

When I decorated my property with the DataType attribute, I did not add MaxLength.  I'm sure that allows you to enter however many characters you want.  BUT, my issue is, if you do NOT decorate the property, and only specify that it is a string, it will allow you to enter as many characters as you want.  If you decorate it with the DataType.MultilineText, it won't allow over 128 characters.  Why?  Pure logic tells me that if I'm using a multiline (textarea) then I would want to enter more than I would with a single line textbox.

Mar 29, 2011 at 1:25 PM

I did some testing and I get the error with or without the attribute if I put in > 128 characters. It happens on the context.Save() in the repository. I also get it with 

[DataType(DataType.MultilineText)] and 
[DataType(DataType.Text)]

This seems like a reasonable error since the DB can't take that value.
Mar 29, 2011 at 2:08 PM

Not quite my point.  If you just simply create a property in a class:

public class Post{
    public int ID { get; set; }
    public string Text {get; set; }
}

The value of text will allow you to enter > 128 characters.  When you decorate the property with the attribute with the datatype of Text or MultilineText, it does not allow > 128 characters:

public class Post{
    public int ID { get; set; }

    [DataType(DataType.MultilineText)]
    public string Text {get; set; }
}
So, my point was, without decoration, your entry is not affected. You are allowed to enter as many characters as needed (I'm sure there's a limit but it's much more than 128 characters). If you DO decorate the property, it doesn't allow > 128 characters.  This is a bug.