代码之家  ›  专栏  ›  技术社区  ›  Lwrnc Crz

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

  •  0
  • Lwrnc Crz  · 技术社区  · 7 年前

    我想使用这个InstitutionName作为查询来显示搜索结果,但我不想在结果表中的每一行重复出现,我只想将其显示为标题。我尝试使用DisplayFor,但它总是返回一个错误,我猜是因为我的模型是IEnumerable,但我不知道,我只是暂时输入DisplayNameFor来模拟我想要显示的输出,它应该是属性本身的值,而不仅仅是属性名。

    看法

    @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>
    
    3 回复  |  直到 7 年前
        1
  •  0
  •   Lwrnc Crz    7 年前

    这就是我最初使用@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>
    
        2
  •  0
  •   Farhad Bagherlo    7 年前

    此属性返回 第一 违约

    FirstOrDefault

     @if(Model != null){
        <h2>@Model.FirstOrDefault().Compendium.Institution.InstitutionName</h2>
     }
    
        3
  •  0
  •   Alsamil Mehboob    7 年前

    尝试以下解决方案:

    <h2>@Html.DisplayNameFor(@Model.FirstOrDefault().Compendium.Institution.InstitutionName)</h2>
    

    <h2>@Html.DisplayNameFor(model=>model.Compendium.Institution.InstitutionName)</h2>

    @model IEnumerable<SMOSystemv2.Models.Tender> 按您的代码。财产 FirstOrDefault() 将给您列表中的第一个值作为默认值。