【问题标题】:Two models in a view [duplicate]视图中的两个模型[重复]
【发布时间】:2018-05-19 05:23:22
【问题描述】:

我有一个 cshtml 视图和一些模型来查看列中的值。在 Name4 和 Name5 列中,我想显示第二个模型的值,但我不知道该怎么做。例如,现在我显示模型 Project.Models.Names 中的值,但在 Name4 和 Name5 列中,我想显示模型 Project.Models.Surnames 中的值。

@model IEnumerable<Project.Models.Names>



@{
ViewBag.Title = "Smt";

}

<h2>Smt</h2>


<table class="table">
<tr>
    <th>
        Name
    </th>
    <th>
        Name1
    </th>
    <th>
        Name2
    </th>
    <th>
        Name3
    </th>
    <th>
        Name4
    </th>
    <th>
        Name5
    </th>
    <th>

    </th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Name1)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Name2)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Name3)
    </td>
    <td>
        ...value from second model
    </td>
    <td>
        ...value from second model
    </td>

【问题讨论】:

  • 为什么不创建一个包含姓名和姓氏的模型 Persons?
  • name 和 surname 是为这个问题随机选择的名字

标签: c# razor model ienumerable


【解决方案1】:

创建一个具有 Name 和 Surname 属性的 Viewmodel 类。然后绑定到那个。

【讨论】:

    猜你喜欢
    • 2016-08-30
    • 2017-05-04
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    相关资源
    最近更新 更多