代码之家  ›  专栏  ›  技术社区  ›  Wouter

DataType.EmailAddress和DataType.PhoneNumber的自定义显示模板

  •  1
  • Wouter  · 技术社区  · 13 年前

    我想为数据类型EmailAddress和PhoneNumber创建自定义显示模板。

    DisplayTemplates是使用模型类型解析的。但是,无法在此处解析DataType。我可以将模型类型设置为字符串,为任何字符串创建自定义显示模板,但传入的是字符串,而不是对象的属性。

    1 回复  |  直到 13 年前
        1
  •  3
  •   Darin Dimitrov    13 年前

    然而,传入的是一个字符串,而不是对象的属性。

    是,但您可以访问元数据:

    ViewData.ModelMetadata.DataTypeName
    

    例如,您可以有以下字符串模板:

    @using System.ComponentModel.DataAnnotations
    
    @if (ViewData.ModelMetadata.DataTypeName == DataType.PhoneNumber.ToString())
    { 
        <div>This is a phone number template</div>
    }
    else
    {
        @Html.TextBox(
            "", 
            ViewData.TemplateInfo.FormattedModelValue,
            new { @class = "text-box single-line" }
        )
    }