asp.net-mvc - mvc4 和 EF 中的 Html.EditorFor 和多行文本框

标签 asp.net-mvc entity-framework razor

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

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

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

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

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

干杯 蒂姆

最佳答案

我想我已经有了部分答案。通过阅读更多有关 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 语法的要求。

蒂姆

关于asp.net-mvc - mvc4 和 EF 中的 Html.EditorFor 和多行文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10362333/

相关文章:

entity-framework - 如何使用linq按星期几分组

asp.net-mvc - PartialView 必须从 WebViewPage 错误派生

asp.net-mvc - 使用 data-val ="true"是正确的方法,强制进行所需的验证

c# - ASP.NET MVC 发布不适用于弹出窗口

entity-framework - 使用 EF 6 alpha3 Code First 和 Migrations 创建 __MigrationHistory 表部署到 SQL Azure 时出错

asp.net-mvc - 在没有 View 的情况下运行 MVC Controller 操作?

WPF/MVVM/EF - 如何绑定(bind)到实体的相关实体?

asp.net-core - 检查 RenderFragment 是否为空

c# - 如何将 Angular POST 发送到 C# Asp.Net MVC Controller ?

c# - 无法在现有页面中使用挖空模板