【问题标题】:What does the model parameter mean in case of strongly typed container model在强类型容器模型的情况下,模型参数是什么意思
【发布时间】:2016-10-13 17:09:54
【问题描述】:

下面的strongly typed models syntax不太明白:

@model IEnumerable<MvcMovie.Models.Movie>

@{
    ViewData["Title"] = "Index";
}

<h2>Index</h2>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Genre)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Price)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ReleaseDate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Title)
            </th>
            <th></th>
        </tr>
    </thead>

我们为什么要这样访问Genre 属性?

@Html.DisplayNameFor(model => model.Genre)

我以为这里的model指的是整个容器:

@model IEnumerable<MvcMovie.Models.Movie>

我错了吗?

【问题讨论】:

标签: c# asp.net razor


【解决方案1】:

您是否尝试执行您显示的代码?我不认为它会起作用。看起来您链接的教程向您显示了一个 div(使用黄色突出显示)以清除 @model MvcMovie.Models.Movie@model IEnumerable&lt;MvcMovie.Models.Movie&gt; 之间的区别。

所以正确的方法是:

@model IEnumerable<MvcMovie.Models.Movie>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Genre)
            </td>
         @* ... *@
        </tr>
}

【讨论】:

  • 不,它有效。我强烈建议您再次查看我提供的链接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-31
  • 1970-01-01
  • 1970-01-01
  • 2012-04-19
  • 2011-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多