修改 TT 模板以添加所需的 html 元素
我正在尝试创建 t4 模板来帮助加快我的创建表单模板的速度。
是否可以根据模型的属性是否需要添加额外的 html? 例如,
[Required]
[Display(Name = "Contact Email Address:")]
public string ContactEmailAddress { get; set; }
现在在我的 tt 文件中执行类似的操作
foreach (ModelProperty property in GetModelProperties(mvcHost.ViewDataType)) {
if (!property.IsPrimaryKey && !property.IsReadOnly) {
#>
<div>
@Html.LabelFor(model => model.<#= property.Name #>)
@Html.EditorFor(model => model.<#= property.Name #>)
@Html.ValidationMessageFor(model => model.<#= property.Name #>)
if(this.Required==true){<span class="required-field"></span>}
</div>
<#
}
或者这不可能吗?
I am trying to create a t4 template to help speed up my Create Form template.
Is it possible to add extra html depending if a model's property is required?
e.g.
[Required]
[Display(Name = "Contact Email Address:")]
public string ContactEmailAddress { get; set; }
Now in my tt file do something like
foreach (ModelProperty property in GetModelProperties(mvcHost.ViewDataType)) {
if (!property.IsPrimaryKey && !property.IsReadOnly) {
#>
<div>
@Html.LabelFor(model => model.<#= property.Name #>)
@Html.EditorFor(model => model.<#= property.Name #>)
@Html.ValidationMessageFor(model => model.<#= property.Name #>)
if(this.Required==true){<span class="required-field"></span>}
</div>
<#
}
Or is this not possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
1、打开此文件:
2、向 ModelProperty 添加一些属性
3、在该类下添加一个方法
4、转到该文件的底部修改方法 GetEligibleProperties:
5、转到该文件
在 ValidationMessageFor 之前添加以下检查
像这样的代码:
1,Open this file:
2, Add some property to ModelProperty
3, Add an method out under this class
4,Go to the bottom of this file to modify the method GetEligibleProperties:
5, go to the file
Add the following check before your ValidationMessageFor
Codes like this:
这是可能的,但需要比您这里所做的更多工作。您可以将必需的属性添加到 ModelProperty,然后通过在获取模型属性时查找属性上的必需属性来设置它。您可以查看 .tt 模板中确定属性是否为主键的代码作为示例。
This would be possible but would take more work than what you have here. You could add a Required property to ModelProperty and then set it by looking for the required attribute on the property when getting the model properties. You can take a look at the code that determines whether or not a property is a primary key in the .tt templates for an example.
如果有人仍在寻找解决方案...
我正在使用 MetadataType 属性在单独的类中定义属性属性,例如“Required”或“DisplayName”。
示例:
如果要访问 t4 模板中的这些属性,则必须扩展模板文件中的 ModelProperty 类和创建者。
将以下代码放在模板的底部(例如List.tt)。您必须替换现有的代码。
现在您可以像这样访问这些属性:
If someone still searching for a solution...
I'm using the MetadataType attibute to define the property attributes like Required or DisplayName in a seperate class.
Sample:
If you want to access these attributes in the t4-template you have to extend the ModelProperty class and creator in the template-file.
Put the following code at the bottom of your template (e.g. List.tt). You have to replace the existing code.
Now you can access these attributes like this:
T4 模板已随 MVC5 发生变化。为了在 MVC5 中完成此任务,我在这里编写了一个教程: https://johniekarr.wordpress.com/2015/05/16/mvc-5-t4-templates-and-view-model-property-attributes/
T4 templates have changed with MVC5. To accomplish this in MVC5, I've written a tutorial here: https://johniekarr.wordpress.com/2015/05/16/mvc-5-t4-templates-and-view-model-property-attributes/