asp.net-mvc - DisplayForModel 的问题

标签 asp.net-mvc asp.net-mvc-3

我有以下模型:

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/

相关文章:

c# - 将 Id 或其他属性添加到 ASP.NET MVC 3 Html Helper Textbox

c# - 领域模型上的切换逻辑

ASP.net:由于数据库错误,无法使用网站管理工具

c# - 带有脚本的 HtmlHelper 扩展

c# - 路由中的空间给出 404

.net - 是否有类似 .NET 开发的 Ruby on Rails 框架?

asp.net-mvc - asp.net mvc - ActionLink 的呈现不一致

javascript - 选择器中的 jquery 字符串连接

c# - ModelState.AddModelError 在表单级别与 ASP.NET MVC 2 RC

c# - 拆分字符串时出现混淆错误