C# DisplayForModel()不显示复杂对象+;MVC4
我有以下型号:C# DisplayForModel()不显示复杂对象+;MVC4,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我有以下型号: public class Car { public int Id { get; set; } public string Colour { get; set; } public Category Category { get; set; } } public class Category { public int CatId { get; set; } public string Name { get; set; } } 我返回模型以查看
public class Car
{
public int Id { get; set; }
public string Colour { get; set; }
public Category Category { get; set; }
}
public class Category
{
public int CatId { get; set; }
public string Name { get; set; }
}
我返回模型以查看,如下所示:
public ActionResult Index()
{
var car = new Car { Id = 1, Colour = "Black", Category = new Category { CatId = 2, Name = "Audi" } };
return View(car);
}
我的看法是:
@model MvcApplication6.Models.Car
@{
ViewBag.Title = "Car";
}
@Html.DisplayForModel()
这不显示类别。
我发现这是MVC中的一个bug,作为一种解决方法,我需要覆盖Object.ascx displaytemplate
所以我在Views/Shared/DisplayTemplates/folder中添加了object.cshtml,代码如下
@functions{
public bool ShouldShow(ModelMetadata metadata)
{
//return number % 2 == 0 ? true : false;
return metadata.ShowForDisplay
&& metadata.ModelType != typeof(System.Data.EntityState)
&& !metadata.IsComplexType
&& !ViewData.TemplateInfo.Visited(metadata);
}
}
@if (Model == null) {
@(ViewData.ModelMetadata.NullDisplayText)
} else if (ViewData.TemplateInfo.TemplateDepth > 1) {
@(ViewData.ModelMetadata.SimpleDisplayText)
} else {
foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => ShouldShow(pm))) {
if (prop.HideSurroundingHtml) {
@(Html.Display(prop.PropertyName))
} else {
if (!String.IsNullOrEmpty(prop.GetDisplayName())) {
<div class="display-label">@(prop.GetDisplayName())</div>
}
<div class="display-field">@(Html.Display(prop.PropertyName))</div>
}
}
}
@函数{
公共bool ShouldShow(模型元数据)
{
//返回编号%2==0?真:假;
return metadata.ShowForDisplay
&&metadata.ModelType!=typeof(System.Data.EntityState)
&&!metadata.IsComplexType
&&!ViewData.TemplateInfo.Visited(元数据);
}
}
@if(Model==null){
@(ViewData.ModelMetadata.NullDisplayText)
}否则如果(ViewData.TemplateInfo.TemplateDepth>1){
@(ViewData.ModelMetadata.SimpleDisplayText)
}否则{
foreach(ViewData.ModelMetadata.Properties.Where(pm=>ShouldShow(pm))中的var prop){
if(项目隐藏周围TML){
@(Html.Display(prop.PropertyName))
}否则{
如果(!String.IsNullOrEmpty(prop.GetDisplayName())){
@(prop.GetDisplayName())
}
@(Html.Display(prop.PropertyName))
}
}
}
然而,catogery仍然没有显示出来,有人能提供建议吗?Hm,我做的一切都像你一样,从这里获得了模板的代码: HomeController.cs
public class HomeController : Controller
{
//
// GET: /Home/
public class Car
{
public int Id { get; set; }
public string Colour { get; set; }
public Category Category { get; set; }
}
public class Category
{
public int CatId { get; set; }
public string Name { get; set; }
}
public ActionResult Index()
{
var car = new Car { Id = 1, Colour = "Black", Category = new Category { CatId = 2, Name = "Audi" } };
return View(car);
}
}
Index.cshtml
@model MvcApplication1.Controllers.HomeController.Car
@{
ViewBag.Title = "Car";
}
@Html.DisplayForModel()
查看/共享/显示模板/Object.ascx
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<% if (Model == null) { %>
<%= ViewData.ModelMetadata.NullDisplayText %>
<% }
else if (ViewData.TemplateInfo.TemplateDepth > 1) { %>
<%= ViewData.ModelMetadata.SimpleDisplayText %>
<% }
else { %>
<% foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay
&& !ViewData.TemplateInfo.Visited(pm))) { %>
<% if (prop.HideSurroundingHtml) { %>
<%= Html.Display(prop.PropertyName) %>
<% }
else { %>
<% if (!String.IsNullOrEmpty(prop.GetDisplayName())) { %>
<div class="display-label"><%= prop.GetDisplayName() %></div>
<% } %>
<div class="display-field"><%= Html.Display(prop.PropertyName) %></div>
<% } %>
<% } %>
<% } %>
1) { %>
pm.ShowForDisplay
&&!ViewData.TemplateInfo.Visited(pm)){%>
我有一个结果:
编辑:
顺便说一下,如果从DisplayTemplate中删除此代码:
if (ViewData.TemplateInfo.TemplateDepth > 1) { %>
<%= ViewData.ModelMetadata.SimpleDisplayText %>
<% }
if(ViewData.TemplateInfo.TemplateDepth>1){%>