2012-04-28 33 views
3

自動檢測@HTMLEditorFor的標準是什麼?該模型的數據類型將更適合顯示在多行文本框中?使用MVC4,剃鬚刀和EF4.3 DatabaseFirst。我正在使用控制器嚮導腳手架頁面進行CRUD操作。 Edit.cshtml和Create.cshtml都使用mvc4和EF中的Html.EditorFor和多行文本框

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

要顯示文本框。如果我編輯腳手架Myemail.cs類,我可以添加一個DataAnnotation像

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

我現在得到產生<TextArea>(儘管是一個不切實際的大小,使用(一條線和178px)。如果不是這種自動啓動時TT模板生成模式?或者我需要以某種方式修改TT模板承擔<TextArea>如果該字段是一個varchar等具有尺寸大於說100?

乾杯 添

回答

3

認爲我有一部分我的答案。通過閱讀更多關於TextTemplatesTransformations我已經修改了我的Model1.tt包括:

System.ComponentModel.DataAnnotations; 

我還修改了寫屬性的方法來接受EdmPropertyType。代碼現在爲所有來自指定長度> 60或最大定義的字符串的字符串生成多行註釋。它還會生成一個最大長度註釋,它應該有助於防止溢出。如果使用您將需要修改現有的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語法的要求。

Tim