ele*_*tor 1 asp.net-mvc t4 customization data-annotations
您是否可以为模型创建自定义数据注释,可以在T4模板中读取类似于View的属性.是否读取了文件夹?我想添加数据注释参数,比如我将构建视图的Scaffold.
谢谢
我写了一篇关于我为MVC5提出的解决方案的博客文章.我在这里发布它给任何出席的人:https: //johniekarr.wordpress.com/2015/05/16/mvc-5-t4-templates-and-view-model-property-attributes/
编辑:在您的实体中,使用自定义属性装饰属性
namespace CustomViewTemplate.Models
{
[Table("Person")]
public class Person
{
[Key]
public int PersonId { get; set;}
[MaxLength(5)]
public string Salutation { get; set; }
[MaxLength(50)]
public string FirstName { get; set; }
[MaxLength(50)]
public string LastName { get; set; }
[MaxLength(50)]
public string Title { get; set; }
[DataType(DataType.EmailAddress)]
[MaxLength(254)]
public string EmailAddress { get; set; }
[DataType(DataType.MultilineText)]
public string Biography { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
使用此自定义属性
namespace CustomViewTemplate
{
[AttributeUsage(AttributeTargets.Property)]
public class RichTextAttribute : Attribute
{
public RichTextAttribute() { }
}
}
Run Code Online (Sandbox Code Playgroud)
然后创建一个我们将在模板中引用的T4Helper
using System;
namespace CustomViewTemplate
{
public static class T4Helpers
{
public static bool IsRichText(string viewDataTypeName, string propertyName)
{
bool isRichText = false;
Attribute richText = null;
Type typeModel = Type.GetType(viewDataTypeName);
if (typeModel != null)
{
richText = (RichTextAttribute)Attribute.GetCustomAttribute(typeModel.GetProperty(propertyName), typeof(RichTextAttribute));
return richText != null;
}
return isRichText;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2493 次 |
| 最近记录: |