【问题标题】:Accessing child collection in MVC5 razow views from model从模型访问 MVC 5 剃刀视图中的子集合
【发布时间】:2018-05-08 14:36:54
【问题描述】:

我目前在尝试访问我的模型中的子集合时遇到问题。

我的模型如下所示(请记住,我已经剥离了很多值,我的模型并没有那么小):

public class Games
{
    public int Id { get; set; }
    public string Title { get; set; }
    public ICollection<GameDetails> Details { get; set; }
}

现在我可以使用简单的 linq 查询轻松获取控制器中的数据并包括子数据(在本例中为“详细信息”)。

然而,我正在努力了解如何访问这些数据以进行发布以及在将其传递回我的视图时进行读取。

我的观点是这样的:

<dt>
        @Html.DisplayNameFor(model => model.Title)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Title)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.Details)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Details)
    </dd>

但我似乎无法访问子集合中的数据。

我已经尝试过model.Details。但我无法访问子集合中的数据。

任何帮助将不胜感激。

【问题讨论】:

    标签: razor model-view-controller asp.net-core-2.0


    【解决方案1】:

    我认为问题不在于使用List 而不是ICollection

    • 阅读:您可以使用@foreach@for() 来迭代集合中的项目。
    • 发布:这是在使用@foreachHtmlHelperTagHelper 在表单内生成输入时不起作用,因为@foreach 将为这些输入生成相同的ID。因此,您必须将@for() 与索引一起使用。

    我还看到你使用了@Html.DisplayFor()。我假设你知道你在做什么。 @Html.DisplayFor() 将呈现与属性类型匹配的名为 DisplayTemplates 的模板。

    例如,假设属性TitleString,如果您在视图的DisplayTemplates 文件夹中定义了一个名为String.cshtml 的视图,则调用@Html.DisplayFor(x =&gt; x.Title) 将使用该模板来呈现该属性.

    String.cshtml

    @model string
    
    @if (String.IsNullOrEmpty(Model))
    {
        <!-- You can render different HTML elements here -->
    }
    else
    {
        <!-- You can render different HTML elements here -->
    }
    

    我通常不使用@Html.DisplayFor() 来构建表单元素。相反,我只会使用特定的 HTML 助手或标签助手来构建特定的输入类型。例如,要构建文本框,请使用 @Html.TextBoxFor()&lt;input type="text" asp-for="xxx" /&gt;。要构建文本区域,请使用 @Html.TextAreaFor()&lt;textarea asp-for="xxx"&gt;&lt;/textarea&gt;

    我只是想显式声明/定义输入元素。

    使用标签助手的示例表单(手写,未经测试):

    @model Games
    
    <!-- asp-area, asp-controller, asp-action are tag helpers on the form -->
    <form asp-area="" asp-controller="game" asp-action="manage">
        <!-- hidden type input for the Id? -->
        <input type="hidden" asp-for="Id" />
        <dl>
            <dt>
                <label asp-for="Title"></label>
            </dt>
            <dd>
                <input type="text" asp-for="Title" />
            </dd>
            <dt>
                <label asp-for="Details"></label>
            </dt>
            <dd>
                @for(int i = 0; i < Model.Details.Count(); i++)
                {
                    <label asp-for="Details[i].MakeUpProperty1"></label>
    
                    <!-- textbox for property 1? -->
                    <input type="text" asp-for="Details[i].MakeUpProperty1" />
    
                    <label asp-for="Details[i].MakeUpProperty2"></label>
    
                    <!-- number type input box for property 2? -->
                    <input type="number" asp-for="Details[i].MakeUpProperty2" />
    
                    <!-- dropdown for property 3? -->
                    <select asp-for="Details[i].MakeUpProperty3"
                        asp-items="Model.AvailableProperty3List.ToSelectListItem()" ></select>              
                }
            </dd>
        </dl>
    </form>
    

    【讨论】:

      【解决方案2】:

      在列表中使用@for 循环

      @for (int i = 0; i < model.Details.Count(); i++)
      {
          <dt>
              @Html.DisplayNameFor(model => model.Details[i].Title)
          </dt>
          ...
      }
      

      最好在模型中使用 List 而不是 ICollection - 因此视图模型包含要显示的项目的最终列表。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-29
        • 1970-01-01
        • 2015-03-29
        • 1970-01-01
        • 2012-02-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多