我在 MVC 5 应用程序中遇到了自定义显示模板的问题。我有一个包含我想重用的复杂类型的视图模型。我创建了 DisplayTemplates 文件夹并放置了一个遵循正确命名约定的局部视图。一切似乎都正常工作,局部视图被调用,但浏览器上没有呈现任何内容。
我检查了渲染的 html 以确保它没有被隐藏或显示有趣,但 html 输出中实际上没有渲染任何内容。请参阅下面的相关截图。我究竟做错了什么?谢谢。
文件夹结构
这是最初的看法。视图模型有一个可用选项列表。foreach 调用 displayfor
@model CustomerWebPortal.ViewModels.OrderVehicle
@using (Html.BeginForm("CreateOrder", "Home"))
{
<h2>@Html.DisplayFor(x => x.DisplayName)</h2>
<div class="description">
<ul>
@Html.Raw(Model.Description)
</ul>
</div>
<h3>Available Options</h3>
<hr />
<div>
@foreach (var option in Model.AvailableOptions)
{
Html.DisplayFor(x => option, "AvailableOption");
}
</div>
<h3>Customer</h3>
<hr />
<div class="customerInfo">
@Html.LabelFor(x => x.Customer)
@Html.TextBoxFor(x => x.Customer)
@Html.ValidationMessageFor(x => x.Customer)
</div>
<div class="actions">
<input type="submit" value="Place Order" />
</div>
@Html.HiddenFor(x => x.VehicleId)
@Html.HiddenFor(x => x.Price)
}
这是复杂类型的部分视图
@model CustomerWebPortal.ViewModels.AvailableOption
<div class="row-fluid padtopbottom5">
<div class="span6">
<div class="row-fluid">
<div class="span4">
@Html.CheckBoxFor(x => x.Selected)
</div>
</div>
</div>
<div class="span6">
<div class="row-fluid">
<div class="span4">
<h3>@Html.DisplayFor(x => x.DisplayName)</h3>
<ul>
@Html.Raw(Model.Description)
</ul>
<p><strong>@Html.DisplayFor(x => x.Price)</strong></p>
</div>
</div>
</div>
</div>
这是为复杂类型调用 displayfor 的 foreach
这是生成的渲染 HTML
我创建了一个小型测试项目,如果有人想看一下,它会重现错误。示例项目