3

@HTMLEditorFor自动检测模型的数据类型是否更适合显示在多行文本框中的标准是什么?使用 MVC4、razor 和 EF4.3 DatabaseFirst。我正在使用控制器向导搭建页面进行 CRUD 操作。Edit.cshtml 和 Create.cshtml 都在使用

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

显示文本框。如果我编辑脚手架 Myemail.cs 类,我可以添加一个 DataAnnotation

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

我现在得到一个<TextArea>生成的(尽管开始使用的大小不切实际(一条线和 178 像素)。当 tt 模板生成模型时这不应该是自动的吗?或者我是否需要以某种方式修改 tt 模板以假设<TextArea>字段是varchar大小大于 100的等?

干杯蒂姆

4

1 回答 1

3

认为我有部分答案。通过阅读更多关于 TextTemplatesTransformations 我修改了我的 Model1.tt 以包括:

System.ComponentModel.DataAnnotations;

我还修改了 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 语法的要求。

蒂姆

于 2012-04-29T10:21:20.337 回答