gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:04:21 25 4
gpt4 key购买 nike

我有以下模型:

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 显示模板。

所以我在 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>
}
}
}

但是仍然没有显示目录,有人可以建议吗?

最佳答案

嗯,我做的一切都像你一样,并从这里获得了模板代码: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()

Views/Shared/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,您只能看到标识属性,而不是完整的对象。

关于c# - DisplayForModel() 不显示复杂对象 + MVC4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17540443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com