Razor 使用T4模板创建带有自定义Html控件的视图
我创建了视图模型类,如bellowRazor 使用T4模板创建带有自定义Html控件的视图,razor,template-engine,t4,envdte,asp.net-mvc-scaffolding,Razor,Template Engine,T4,Envdte,Asp.net Mvc Scaffolding,我创建了视图模型类,如bellow public class GenericFormModel { public string SimpleInput { get; set; } public string InputAddOn { get; set; } public string InputTextArea { get; set; } public DateRange Date { get; set; } } 然后,我在rote/codemplates/
public class GenericFormModel
{
public string SimpleInput { get; set; }
public string InputAddOn { get; set; }
public string InputTextArea { get; set; }
public DateRange Date { get; set; }
}
然后,我在rote/codemplates/MvcView位置下创建了自己的创建模板CustomCreate.cs.t4。我尝试为DateRange属性创建输入Razor控件。但我无法从CustomCreate.cs.t4读取DateRange属性,如下所示
<#
foreach (PropertyMetadata property in ModelMetadata.Properties) {
if (property.Scaffold && !property.IsAutoGenerated && !property.IsReadOnly && !property.IsAssociation) {
if (property.IsPrimaryKey && IsPropertyGuid(property)) {
continue;
}
#>
@Html.InputFor(model => model.<#= property.PropertyName #>)
<#
}
}
#>
@InputFor(model=>model.)
注:
我的问题是ModelMetadata。属性仅返回字符串、int、DateTime、decimal..仅返回属性。它没有返回DateRange属性
我犯了什么错误?我使用的是VS2013和MVC5
InputFor()是我的自定义Html帮助程序
ModelMetadata.Properties
是ModelMetadata对象的ienumerable,而不是PropertyMetadata(从文档中)找不到PropertyMetadata的文档,不知道强制转换为什么不抛出,所以我无法告诉您。调试它,设置一个断点并检查ModelMetadata.Properties
以查看其中的内容。是的,您正在编写,是否有其他方法来执行此操作。我不知道,因为它应该在运行时爆炸。在运行时检查对象,看看可以从中得到什么。