Html.EditorFor and multi-line text fields in mvc4 and EF

What are the criteria for automatically determining @HTMLEditorForthat the data type of the model will be more appropriately displayed in a multi-line text box? Using MVC4, razors and EF4.3 DatabaseFirst. I use controller master classes to work with CRUD. The files Edit.cshtml and Create.cshtml use

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

To display a text box. If I edit the Myemail.cs class, I can add a DataAnnotation, for example

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

Now I get the created <TextArea>(albeit impractical size to start using (one line and 178px). If this is not automatic when the tt templates generate the models? Or do I need to somehow change the tt templates suppose that <TextArea>if the field is varcharand t .d. with a size greater than 100?

Cheers Tim

+3
source share
1 answer

I think I have a part of my answer. Reading a lot more about TextTemplatesTransformations, I changed my Model1.tt to include:

System.ComponentModel.DataAnnotations;

i WriteProperty, EdmPropertyType. , > 60 . maxlength, , , . , WriteProperty, .

void WriteProperty(CodeGenerationTools code, EdmProperty edmProperty)
{
    WriteProperty(Accessibility.ForProperty(edmProperty),
                  code.Escape(edmProperty.TypeUsage),
                  code.Escape(edmProperty),
                  code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
                  code.SpaceAfter(Accessibility.ForSetter(edmProperty)),edmProperty);
}



void WriteProperty(string accessibility, string type, string name, string getterAccessibility, string setterAccessibility,EdmProperty edmProperty = null)
{
    if (type =="string")    
    {
        int maxLength = 0;//66
        bool bres = (edmProperty != null
            && Int32.TryParse(edmProperty.TypeUsage.Facets["MaxLength"].Value.ToString(), out maxLength));
        if (maxLength > 60) // want to display as a TextArea in html 
        {
#> 
    [DataType(DataType.MultilineText)]
    [MaxLength(<#=maxLength#>)]
<#+
        }
        else
        if (maxLength < 61 && maxLength > 0) 
        {
#> 
    [MaxLength(<#=maxLength#>)]
<#+
        }
        else
        if(maxLength <=0) //varchar(max)
        {
#> 
    [DataType(DataType.MultilineText)]
<#+
        }

    }
#>
    <#=accessibility#> <#=type#> <#=name#> { <#=getterAccessibility#>get; <#=setterAccessibility#>set; }
<#+
}

, < # + # > , , TT.

+3

All Articles