【发布时间】:2015-03-31 19:08:50
【问题描述】:
我有视图模型
public class ClientIndexViewModel
{
[Key]
public int SurrogateId { get; set; } // not primary key just used in templating
public IEnumerable<Client> Clients { get; set; }
}
我创建了一个名为Index.cs.t4 的新MvcView t4 模板。当我从控制器创建一个新视图并将我的模型类添加为 ClientIndexViewModel 时,会生成视图模板 ( index.cshtml ),但没有来自 IEnumerable<Client> 的属性
模板化索引.cshtml
<table class="table">
<!-- Client properties should be here .. i think -->
<tr>
<th></th> <!-- This is all empty because i dont really know what i am doing -->
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.SurrogateId }) |
@Html.ActionLink("Details", "Details", new { id=item.SurrogateId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.SurrogateId })
</td>
</tr>
}
当我查看模板文件时,这是有道理的。
Index.cs.t4
<#
IEnumerable<PropertyMetadata> properties = ModelMetadata.Properties;
foreach (PropertyMetadata property in properties) {
if (property.Scaffold && !property.IsPrimaryKey && !property.IsAssociation) {
#>
<#
if (property.IsAssociation && GetRelatedModelMetadata(property) == null) {
continue;
}
#>
<th>
@Html.DisplayNameFor(model => model.<#= GetValueExpression(property) #>)
</th>
<#
}
}
#>
我希望能够专门针对properties 变量的视图模型上的IEnumerable<Client> 属性,作为PropertyMetaData 上的枚举属性
我想我可以在ModelMEtadataFunctions.include.t4 文件中创建一个方法。只是不太确定如何,或者我什至应该这样做
【问题讨论】:
-
您确定将 ClientIndexViewModel 作为模型添加到 Templated Index.cshtml 中吗?
-
@MuratYıldız 作为对话框中的类模型?是的。
标签: asp.net-mvc t4 scaffolding asp.net-mvc-scaffolding