DisplayForModel的问题

时间:2012-01-15 16:09:58

标签: 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>

当我的视图出现时,我会看到AdminDetails记录的数据,但<tr><td>没有格式,我也看不到任何x。我正在使用区域,视图的代码在区域内。是否有可能我的代码没有获得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>

1 个答案:

答案 0 :(得分:9)

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

Views\DisplayTemplates\AdminDetail.cshtml

你应该使用:

Views\Shared\DisplayTemplates\AdminDetail.cshtml

注意Shared位。它也可能是:

Views\XXX\DisplayTemplates\AdminDetail.cshtml

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

还要替换你的循环:

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

及其等价物:

@Html.DisplayForModel()

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