gpt4 book ai didi

asp.net-mvc - DisplayForModel 的问题

转载 作者:行者123 更新时间:2023-12-01 21:12:18 27 4
gpt4 key购买 nike

我有以下模型:

namespace Storage.Models
{
public class AdminDetail
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public string Title { get; set; }
public string Status { get; set; }
public string Type { get; set; }
public string Level { get; set; }

public string Order { get; set; }
public int Row { get; set; }
}

在我看来有以下代码:

@model IList<AdminDetail>

<table>
<thead>
<tr>
<th>@Html.LabelFor(x => x[0].Order)</th>
<th>@Html.LabelFor(x => x[0].Order)</th>
<th>@Html.LabelFor(x => x[0].Level)</th>
<th>@Html.LabelFor(x => x[0].Title)</th>
<th>@Html.LabelFor(x => x[0].Status)</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
@Html.DisplayFor(model => item)
}
</tbody>
</table>

在文件中:Views\DisplayTemplates\AdminDetail.cshtml

    @model AdminDetail


xxxxxxxxxxxxxxxxxxxxxxxx
<tr>
<td>@Html.DisplayFor(x => x.Order)</td>
<td>@Html.DisplayFor(x => x.Level)</td>
<td class="indent_@(adminDetail.Level)">@Html.DisplayFor(x => x.Title)</td>
<td>@Html.DisplayFor(x => x.Status)</td>
<td>@Html.ActionLink("Edit", "Edit",
new { pk = adminDetail.PartitionKey, rk = adminDetail.RowKey },
new { @class = "editLink" } )</td>
<td>@Ajax.ActionLink("Delete", "Delete", "Menus",
new { pk = adminDetail.PartitionKey, rk = adminDetail.RowKey },
new AjaxOptions {
HttpMethod = "POST",
Confirm = "Are you sure you want to delete menu item ",
OnSuccess = "deleteConfirmation"
})</td>
<td>@Html.DisplayFor(x => x.PartitionKey) @Html.DisplayFor(x => x.RowKey)</td>
</tr>

当我的 View 出现时,我看到了 AdminDetails 记录的数据,但没有 <tr> 的格式和 <td>我也没有看到任何 x。我正在使用区域, View 代码在一个区域内。是否有可能是因为我正在使用区域,所以我的代码没有获得 DisplayTemplate?

这是数据显示方式的示例:

<table>
<thead>
<tr>
<th><label for="">Order</label></th>
<th><label for="">Order</label></th>

<th><label for="">Level</label></th>
<th><label for="">Title</label></th>
<th><label for="">Status</label></th>
</tr>
</thead>
<tbody>
<div class="display-label">PartitionKey</div>
<div class="display-field">01</div>
<div class="display-label">RowKey</div>
<div class="display-field">02</div>
<div class="display-label">Title</div>
<div class="display-field">Test55</div>
<div class="display-label">Status</div>
<div class="display-field">New</div>
<div class="display-label">Type</div>
<div class="display-field"></div>
<div class="display-label">Level</div>
<div class="display-field"></div>

最佳答案

您的显示模板路径错误。而不是:

Views\DisplayTemplates\AdminDetail.cshtml

你应该使用:

Views\Shared\DisplayTemplates\AdminDetail.cshtml

注意 Shared 位。也可以是:

Views\XXX\DisplayTemplates\AdminDetail.cshtml

如果您希望此显示模板仅适用于此 Controller ,则 XXX 是当前 Controller 的名称。

同时替换你的循环:

@foreach (var item in Model)
{
@Html.DisplayFor(model => item)
}

与其等效:

@Html.DisplayForModel()

你不需要循环。由于您的模型是一个集合,ASP.NET MVC 将自动为该集合的每个元素调用您的显示模板。

关于asp.net-mvc - DisplayForModel 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8871139/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com