在与列表相同的视图中显示模型中的单个项目
Display a single item from the Model in the same view as a list
我想显示使用此 InstitutionName 作为查询的搜索结果,但我不想在结果 table 的每一行中重复出现,我只想将其显示为标题。我尝试使用 DisplayFor 但它总是 returns 一个错误,我猜是因为我的模型是一个 IEnumerable,但我不知道,我只是暂时放入 DisplayNameFor 来模拟我想要显示的输出,这应该是 属性 本身的值,而不仅仅是 属性 名称。
我读过有关使用 ViewModel 的信息,但我只是想知道我是否可以用我现在拥有的东西来完成它。
查看
@model IEnumerable<SMOSystemv2.Models.Tender>
@{
ViewBag.Title = "Tenders";
}
<h1>Tender History</h1>
<h2>@Html.DisplayNameFor(model => model.Compendium.Institution.InstitutionName)</h2>
<table class="table">
<tr>
<th nowrap>
@Html.DisplayNameFor(model => model.Compendium.Institution.InstitutionName)
</th>
<th nowrap>
@Html.DisplayNameFor(model => model.Compendium.Qualification.Title)
</th>
<th nowrap>
@Html.DisplayNameFor(model => model.Slots)
</th>
<th nowrap>
@Html.DisplayNameFor(model => model.TotalAmount)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td nowrap>
@Html.DisplayFor(modelItem => item.Compendium.Institution.InstitutionName)
</td>
<td nowrap>
@Html.DisplayFor(modelItem => item.Compendium.Qualification.Title)
</td>
<td nowrap>
@Html.DisplayFor(modelItem => item.Slots)
</td>
<td nowrap>
@Html.DisplayFor(modelItem => item.TotalAmount)
</td>
</tr>
}
</table>
这是我最初按照建议使用 @Model.First() 寻找的那种显示,但强烈建议改为实施 ViewModel
@model IEnumerable<SMOSystemv2.Models.Tender>
@{
ViewBag.Title = "Tenders";
}
<h1>Tender History</h1>
<h2>@Model.First().Compendium.Institution.InstitutionName</h2>
<table class="table">
<tr>
<th nowrap>
@Html.DisplayNameFor(model => model.Compendium.Qualification.Title)
</th>
<th nowrap>
@Html.DisplayNameFor(model => model.Slots)
</th>
<th nowrap>
@Html.DisplayNameFor(model => model.TotalAmount)
</th>
<th nowrap>
@Html.DisplayNameFor(model => model.Semester)
</th>
<th nowrap>
@Html.DisplayNameFor(model => model.DateReceived)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td nowrap>
@Html.DisplayFor(modelItem => item.Compendium.Qualification.Title)
</td>
<td nowrap>
@Html.DisplayFor(modelItem => item.Slots)
</td>
<td nowrap>
@Html.DisplayFor(modelItem => item.TotalAmount)
</td>
<td nowrap>
@Html.DisplayFor(modelItem => item.Semester)
</td>
<td nowrap>
@Html.DisplayFor(modelItem => item.DateReceived)
</td>
<td nowrap>
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
@Html.ActionLink("Details", "Details", new { id = item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>
此属性returns列表的第一个选项或选为默认[=的选项12=]
@if(Model != null){
<h2>@Model.FirstOrDefault().Compendium.Institution.InstitutionName</h2>
}
尝试以下解决方案:
<h2>@Html.DisplayNameFor(@Model.FirstOrDefault().Compendium.Institution.InstitutionName)</h2>
而不是
<h2>@Html.DisplayNameFor(model=>model.Compendium.Institution.InstitutionName)</h2>
因为您在代码中使用了 @model IEnumerable<SMOSystemv2.Models.Tender>
。 属性 FirstOrDefault()
将为您提供列表中的第一个值作为默认值。
我想显示使用此 InstitutionName 作为查询的搜索结果,但我不想在结果 table 的每一行中重复出现,我只想将其显示为标题。我尝试使用 DisplayFor 但它总是 returns 一个错误,我猜是因为我的模型是一个 IEnumerable,但我不知道,我只是暂时放入 DisplayNameFor 来模拟我想要显示的输出,这应该是 属性 本身的值,而不仅仅是 属性 名称。
我读过有关使用 ViewModel 的信息,但我只是想知道我是否可以用我现在拥有的东西来完成它。
查看
@model IEnumerable<SMOSystemv2.Models.Tender>
@{
ViewBag.Title = "Tenders";
}
<h1>Tender History</h1>
<h2>@Html.DisplayNameFor(model => model.Compendium.Institution.InstitutionName)</h2>
<table class="table">
<tr>
<th nowrap>
@Html.DisplayNameFor(model => model.Compendium.Institution.InstitutionName)
</th>
<th nowrap>
@Html.DisplayNameFor(model => model.Compendium.Qualification.Title)
</th>
<th nowrap>
@Html.DisplayNameFor(model => model.Slots)
</th>
<th nowrap>
@Html.DisplayNameFor(model => model.TotalAmount)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td nowrap>
@Html.DisplayFor(modelItem => item.Compendium.Institution.InstitutionName)
</td>
<td nowrap>
@Html.DisplayFor(modelItem => item.Compendium.Qualification.Title)
</td>
<td nowrap>
@Html.DisplayFor(modelItem => item.Slots)
</td>
<td nowrap>
@Html.DisplayFor(modelItem => item.TotalAmount)
</td>
</tr>
}
</table>
这是我最初按照建议使用 @Model.First() 寻找的那种显示,但强烈建议改为实施 ViewModel
@model IEnumerable<SMOSystemv2.Models.Tender>
@{
ViewBag.Title = "Tenders";
}
<h1>Tender History</h1>
<h2>@Model.First().Compendium.Institution.InstitutionName</h2>
<table class="table">
<tr>
<th nowrap>
@Html.DisplayNameFor(model => model.Compendium.Qualification.Title)
</th>
<th nowrap>
@Html.DisplayNameFor(model => model.Slots)
</th>
<th nowrap>
@Html.DisplayNameFor(model => model.TotalAmount)
</th>
<th nowrap>
@Html.DisplayNameFor(model => model.Semester)
</th>
<th nowrap>
@Html.DisplayNameFor(model => model.DateReceived)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td nowrap>
@Html.DisplayFor(modelItem => item.Compendium.Qualification.Title)
</td>
<td nowrap>
@Html.DisplayFor(modelItem => item.Slots)
</td>
<td nowrap>
@Html.DisplayFor(modelItem => item.TotalAmount)
</td>
<td nowrap>
@Html.DisplayFor(modelItem => item.Semester)
</td>
<td nowrap>
@Html.DisplayFor(modelItem => item.DateReceived)
</td>
<td nowrap>
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
@Html.ActionLink("Details", "Details", new { id = item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>
此属性returns列表的第一个选项或选为默认[=的选项12=]
@if(Model != null){
<h2>@Model.FirstOrDefault().Compendium.Institution.InstitutionName</h2>
}
尝试以下解决方案:
<h2>@Html.DisplayNameFor(@Model.FirstOrDefault().Compendium.Institution.InstitutionName)</h2>
而不是
<h2>@Html.DisplayNameFor(model=>model.Compendium.Institution.InstitutionName)</h2>
因为您在代码中使用了 @model IEnumerable<SMOSystemv2.Models.Tender>
。 属性 FirstOrDefault()
将为您提供列表中的第一个值作为默认值。