In mvc4 and EF, use Html.EditorFor and multiline textboxes.

asp.net-mvc entity-framework razor

Question

what are the requirements for@HTMLEditorFor auto-detecting that a multiline textbox would be a better display option for the model's datatype? using EF4.3 DatabaseFirst, Razor, and MVC4. For the CRUD tasks, I am utilizing the scaffolded pages provided by the controller wizards. Both the Edit.cshtml and the Create.cshtml use

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

to make the textbox visible. Myemail.cs class scaffolding allows me to add a DataAnnotation like

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

Now I receive a<TextArea> created, but in an unsuitable size for first use (one line and 178px). When the TT templates create the models, shouldn't this be done automatically? or do I need to alter the tt templates in any way to presume a<TextArea> if a field is presentvarchar etc. larger than, let's say, 100?

Cheers Tim

1
3
4/28/2012 9:45:42 AM

Accepted Answer

I believe I have part of the solution. I've changed my Model1.tt to add the following after reading a lot more about TextTemplatesTransformations:

System.ComponentModel.DataAnnotations;

The WriteProperty method has also been changed to take an EdmPropertyType. For all strings that originated from a specified length > 60 or a maximum determined string length, the code now outputs multiline annotations. Additionally, a maxlength annotation is produced, which may potentially aid in preventing overflow. If utilizing, you must make the following changes to both of the current WriteProperty overloads.

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; }
<#+
}

As I believe it is a requirement of the TT syntax, make sure the #+ lines and the #> lines begin at the beginning of the line.

Tim

3
4/29/2012 10:21:20 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow