Sam*_*tar 2 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; }
}
Run Code Online (Sandbox Code Playgroud)
我在视图中有以下代码:
@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>
Run Code Online (Sandbox Code Playgroud)
在文件中: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>
Run Code Online (Sandbox Code Playgroud)
当我的视图出现时,我看到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>
Run Code Online (Sandbox Code Playgroud)
显示模板的路径错误.代替:
Views\DisplayTemplates\AdminDetail.cshtml
Run Code Online (Sandbox Code Playgroud)
你应该使用:
Views\Shared\DisplayTemplates\AdminDetail.cshtml
Run Code Online (Sandbox Code Playgroud)
注意Shared一下.它也可能是:
Views\XXX\DisplayTemplates\AdminDetail.cshtml
Run Code Online (Sandbox Code Playgroud)
XXX如果您希望此显示模板仅适用于此控制器,那么其中是当前控制器的名称.
也替换你的循环:
@foreach (var item in Model)
{
@Html.DisplayFor(model => item)
}
Run Code Online (Sandbox Code Playgroud)
与其等价物:
@Html.DisplayForModel()
Run Code Online (Sandbox Code Playgroud)
你不需要循环.由于您的模型是一个集合,ASP.NET MVC将自动为此集合的每个元素调用您的显示模板.
| 归档时间: |
|
| 查看次数: |
3149 次 |
| 最近记录: |