Reputation: 32768
I have the following model:
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; }
}
I have the following code in my view:
@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>
And in the file: 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>
When my view appears I see the data for the AdminDetails records but there is no formatting for <tr>
and <td>
I also don't see any x's. I am using areas and the code for the view is inside an area. Is it possible that my code is not getting the DisplayTemplate because I am using areas?
Here's an example of the way data appears:
<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>
Upvotes: 2
Views: 3221
Reputation: 1038720
The path to your display template is wrong. Instead of:
Views\DisplayTemplates\AdminDetail.cshtml
you should use:
Views\Shared\DisplayTemplates\AdminDetail.cshtml
Notice the Shared
bit. It could also be:
Views\XXX\DisplayTemplates\AdminDetail.cshtml
where XXX
is the name of the current controller if you want this display template to be available only for this controller.
Also replace your loop:
@foreach (var item in Model)
{
@Html.DisplayFor(model => item)
}
with its equivalent:
@Html.DisplayForModel()
You don't need to loop. Since your model is a collection, ASP.NET MVC will automatically invoke your display template for each element of this collection.
Upvotes: 9