与列表相同的视图中显示单个项目



我想使用此机构名称作为查询显示搜索结果,但是我只想将其显示为标题。我尝试使用displayfor,但它总是返回一个错误,我猜是因为我的模型是一个可感受的,但我不知道,我只是临时放入displayname以模拟我要显示的输出,这应该是属性本身,而不仅仅是属性名称。

我已经阅读了有关使用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 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>

此属性返回列表或选项的选项或选项为 default

firstordEfault

 @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()将以默认值为列表中的第一个值。

最新更新