我有一个视图模型:
public class SitesListViewModel
{
public IEnumerable<Site> Sites { get; set; }
public PagingInfo PagingInfo { get; set; }
}
我有一个观点:
@model .....WebUI.Models.SitesListViewModel
<table class="table table-striped">
<tr>
<th width="100%">
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
<th></th>
<th></th>
</tr>
@foreach (var item in Model.Sites)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>@Html.EditButton("Edit", Url.Action("Edit", new { siteID = item.SiteID }), new ButtonRequirement { ExtraClasses = "btn-xs" })</td>
<td>@Html.ViewButton("View", Url.Action("Details", new { siteID = item.SiteID }), new ButtonRequirement { ExtraClasses = "btn-xs" })</td>
<td>
@using (Html.BeginForm("Delete", "Site"))
{
@Html.Hidden("siteID", item.SiteID)
@Html.DeleteButton("Delete", new ButtonRequirement { ExtraClasses = "btn-xs" })
}
</td>
</tr>
}
</table>
这一行需要知道它正在使用Model.Sites.Name而不仅仅是model.Name ..我该怎么做:
@Html.DisplayNameFor(model => model.Name)
我想象它会是这样的:
@Html.DisplayNameFor(model => model.Sites.Name)
但它不起作用:
The type arguments for method 'System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor<TModel,TValue>(System.Web.Mvc.HtmlHelper<TModel>, System.Linq.Expressions.Expression<System.Func<TModel,TValue>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
答案 0 :(得分:0)
在您的视图模型中使用List<>
IEnumerable<>
的{{1}},for
循环应该可以。
@for (int i = 0; i < Model.Sites.Count; i++)
{
<tr>
<td>
@Html.DisplayFor(modelItem => Model.Sites[i].Name)
</td>
<td>@Html.EditButton("Edit", Url.Action("Edit", new { siteID = Model.Sites[i].SiteID }), new ButtonRequirement { ExtraClasses = "btn-xs" })</td>
<td>@Html.ViewButton("View", Url.Action("Details", new { siteID = Model.Sites[i].SiteID }), new ButtonRequirement { ExtraClasses = "btn-xs" })</td>
<td>
@using (Html.BeginForm("Delete", "Site"))
{
@Html.Hidden("siteID", Model.Sites[i].SiteID)
@Html.DeleteButton("Delete", new ButtonRequirement { ExtraClasses = "btn-xs" })
}
</td>
</tr>
}