DisplayForModel()不显示复杂对象+ MVC4

时间:2013-07-09 05:15:06

标签: 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 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中的一个错误,作为一种解决方法,我需要覆盖Object.ascx displaytemplate。

所以我在Views / Shared / DisplayTemplates /文件夹中添加了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>
        }
    }
}

然而,仍然没有显示catogery,有人可以推荐吗?

1 个答案:

答案 0 :(得分:2)

嗯,我做了像你这样的一切,并从这里获得了模板代码:http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html

<强> 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()

<强>查看/共享/ DisplayTemplates / 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>
        <% } %>
    <% } %>
<% } %>

我有这个结果:

enter image description here

修改

顺便说一下,如果从DisplayTemplate中删除此代码:

if (ViewData.TemplateInfo.TemplateDepth > 1) { %>
    <%= ViewData.ModelMetadata.SimpleDisplayText %>
<% }

你可以看到下一个结果:

enter image description here

发生这种情况,因为当显示模板开始显示Category Category属性时,TemplateDepth变为2,你只看到身份属性,而不是完整对象。