在T4支架中读取的MVC 4自定义数据注释 [英] MVC 4 custom data annotations read in T4 scaffolding

查看:85
本文介绍了在T4支架中读取的MVC 4自定义数据注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为模型创建自定义数据批注,该批注可以在T4模板中的View like属性中读取.读取脚手架吗?我想添加诸如Scaffold之类的数据注释参数,以此为基础构建视图.

Can you create custom data annotations for the model that can be read inside the T4 template for the View like property.Scaffold is read? I would like to add data annotation parameters like Scaffold based on which I would build the view.

谢谢

推荐答案

因此,这就是您的操作方式. 遵循本教程,了解如何创建自定义属性 http://origin1tech.wordpress.com/2011/07/20/mvc-data-annotations-and-custom-attributes/

So, this is how you do it. Follow this tutorial on how to create a custom attribute http://origin1tech.wordpress.com/2011/07/20/mvc-data-annotations-and-custom-attributes/

要在T4脚手架模板中读取此属性值,请首先按此处所述添加模板文件. http ://www.hanselman.com/blog/ModifyingTheDefaultCodeGenerationscaffoldingTemplatesInASPNETMVC.aspx

To read this attribute values in the T4 scaffolding templates, first add the template files as described here http://www.hanselman.com/blog/ModifyingTheDefaultCodeGenerationscaffoldingTemplatesInASPNETMVC.aspx

然后,例如,从AddView文件夹中打开List.tt.此模板创建索引视图.

Then, for example, open List.tt from the AddView folder. This template creates the Index view.

转到模板文件的末尾,找到ModelProperty类的定义.向其中添加属性值(公共字符串MyAttributeValue {get; set;}

Go to the end of the template file and find the definition for class ModelProperty. Add your property value to it ( public string MyAttributeValue { get; set; }

现在在List.tt中稍微走几下,然后找到bool Scaffold(PropertyInfo property)方法.您将需要添加自己的属性属性读取器.对于上述教程,此方法为:

Now go a bit down in the List.tt and find bool Scaffold(PropertyInfo property) method. You will need to add your own attribute property reader. This method, for the above mentioned tutorial, would be:

string OptionalAttributesValueReader(PropertyInfo property){
    foreach (object attribute in property.GetCustomAttributes(true)) {
        var attr = attribute as OptionalAttributes ;
        if (attr != null) {
                return attr.style;
        }
    }
    return String.Empty;
}

然后在文件底部找到方法List GetEligibleProperties(Type type).像这样将您的阅读器添加到其中:

Then find the method List GetEligibleProperties(Type type) at the bottom of the file. Add your reader to it like this:

            ...
            IsForeignKey = IsForeignKey(prop),
            IsReadOnly = prop.GetSetMethod() == null,
            Scaffold = Scaffold(prop),
            MyAttributeValue =  OptionalAttributesValueReader(prop)

当您要使用和读取此属性时,可以像在List.tt中使用Scaffold属性一样进行操作.

When you want to use and read this attribute you can do it like the Scaffold property is used in the List.tt

      List<ModelProperty> properties = GetModelProperties(mvcHost.ViewDataType);
      foreach (ModelProperty property in properties) {
          if (property.MyAttributeValue != String.Empty) {
              //read the value
              <#= property.MyAttributeValue #>  
           }
       }

由于这些类是在我的项目中定义的,因此必须将我的项目dll和名​​称空间添加到List.tt的顶部:

Since these classes are defined in my project, I had to add my project dll and namespace to the top of the List.tt:

     <#@ assembly name="C:\myProjectPath\bin\myMVCproject.dll" #>
     <#@ import namespace="myMVCproject.CustomAttributes" #>

如果模型发生变化,并且需要在脚手架中找到这些新变化,则需要重新构建项目.

If your model changes and you need to find these new changes in the scaffolding, you need to rebuild your project.

希望任何寻求解决方案的人都会发现这很有用.询问是否有不清楚的地方.

Hope anyone looking for the solution will find this useful. Ask if there is anything unclear.

这篇关于在T4支架中读取的MVC 4自定义数据注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆