【问题标题】:ASP.NET MVC Two models in ove view, code changeASP.NET MVC 视图中的两个模型,代码更改
【发布时间】:2017-12-12 06:42:34
【问题描述】:

我有这段代码,它运行良好:

@model IEnumerable<Moviestore.Models.Movie>
@{
ViewBag.Title = "Index";
}

<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.Title)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Genre)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Author)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Year)
    </th>
    <th></th>
</tr>

@foreach (var item in Model) {
if (item.IsDeleted == 0)
{
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Title)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Genre)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Author)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Year)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id = item.MovieID }) |
        @Html.ActionLink("Details", "Details", new { id = item.MovieID }) |
        @Html.ActionLink("Delete", "Delete", new { id = item.MovieID })
    </td>
</tr>
}
}
</table>

但我需要再添加一个模型,我用 Tuple 这样做了:

@using Moviestore.Models;
@model Tuple<Movie, User>

@{
ViewBag.Title = "Index";
}

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
    <th>           
        @Html.DisplayNameFor(tuple => tuple.Item1.Title)
    </th>
    <th>
        @Html.DisplayNameFor(tuple => tuple.Item1.Genre)
    </th>
    <th>
        @Html.DisplayNameFor(tuple => tuple.Item1.Author)
    </th>
    <th>
        @Html.DisplayNameFor(tuple => tuple.Item1.Year)
    </th>
    <th></th>
</tr>


@foreach (var item in Model) {
if (item.IsDeleted == 0)
{
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Title)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Genre)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Author)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Year)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id = item.MovieID }) |
        @Html.ActionLink("Details", "Details", new { id = item.MovieID }) |
        @Html.ActionLink("Delete", "Delete", new { id = item.MovieID })
    </td>
</tr>
}
}
</table>

但现在我的下半部分代码有问题。 来自@foreach

我不知道我需要放什么来代替@foreach(Model中的var item)

然后@Html.DisplayFor(modelItem => item.Title) 也需要一些改动。

抱歉,帖子太长了,我已尽我所能解释问题。

【问题讨论】:

  • 处理这种情况(在特定视图页面上需要多个模型)的适当方法是使用包含所需模型的 ViewModel。
  • 是的。如果您想在视图中使用模型 m1 和 m2,请创建另一个模型 (vm),其中包含 m1 和 m2 的成员。

标签: c# asp.net asp.net-mvc


【解决方案1】:

正如我在 cmets 中提到的,处理这种情况(在特定视图页面上需要多个模型)的适当方法是使用包含所需模型的 ViewModel(请参阅here for reference)。

不过,要回答您的具体问题,您可以按位置访问元组的每个项目,例如如果你的模型是Tuple&lt;Movie, User&gt;,那么你可以通过model.Item1访问Movie对象,通过model.Item2访问User对象等等。

但我强烈建议您采用我链接的 ViewModel 方法。

【讨论】:

    【解决方案2】:

    您好,这是满足您要求的视图模型:

    public class MovieUserViewModel
    {
        public IEnumerable<Movie> Movie { get; set; }
        public IEnumerable<Users> Users { get; set; }
    }
    

    通过上面的viewmodel,你可以轻松访问各个类

    @foreach (var item in Model.Movie)
    
    @foreach (var item in Model.Users)
    

    附加信息:Understanding ViewModel in ASP.NET MVC

    谢谢

    卡提克

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多