[英]How do I make ASP.NET MVC 3 render objects with complex types in the auto-generated views?
I have model classes that have as properties items of complex types (ie, other model classes). 我有一些模型类,它们具有复杂类型的属性项(即其他模型类)。 How can I make it so when I automatically generate views from Visual Studio, those classes (that are included in the top-level class) are displayed appropriately?
当我从Visual Studio自动生成视图时,如何使这些类(包含在顶级类中)正确显示?
Basically, how do I update http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html to ASP.NET MVC 3? 基本上,如何将http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html更新为ASP.NET MVC 3?
TIA, TIA,
Benjy 本杰
Come on, make a little effort yourself and tell what difficulties you have encountered! 来吧,自己做些努力,说出遇到的困难! Otherwise how do you expect to learn something?
否则,您期望如何学习?
Views/Home/Index.cshtml
: Views/Home/Index.cshtml
:
@model SampleModel
<h3>Details</h3>
<fieldset style="padding: 1em; margin: 0; border: solid 1px #999;">
@Html.DisplayForModel()
</fieldset>
<p>@Html.ActionLink("Edit", "Edit")</p>
Views/Home/Edit.cshtml
: Views/Home/Edit.cshtml
:
@model SampleModel
<h3>Edit</h3>
@using (Html.BeginForm())
{
<fieldset style="padding: 1em; margin: 0; border: solid 1px #999;">
@Html.ValidationSummary("Broken stuff:")
@Html.EditorForModel()
<input type="submit" value=" Submit " />
</fieldset>
}
<p>@Html.ActionLink("Details", "Index")</p>
Views/Shared/DisplayTemplates/Object.cshtml
: Views/Shared/DisplayTemplates/Object.cshtml
:
@model object
@if (Model == null)
{
@ViewData.ModelMetadata.NullDisplayText
}
else if (ViewData.TemplateInfo.TemplateDepth > 1)
{
@ViewData.ModelMetadata.SimpleDisplayText
}
else
{
<table cellpadding="0" cellspacing="0" border="0">
@foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm)))
{
if (prop.HideSurroundingHtml)
{
@Html.Display(prop.PropertyName)
}
else
{
<tr>
<td>
<div class="display-label" style="text-align: right;">
@prop.GetDisplayName()
</div>
</td>
<td>
<div class="display-field">
@Html.Display(prop.PropertyName)
</div>
</td>
</tr>
}
}
</table>
}
Views/Shared/EditorTemplates/Object.cshtml
: Views/Shared/EditorTemplates/Object.cshtml
:
@model object
@if (ViewData.TemplateInfo.TemplateDepth > 1)
{
@ViewData.ModelMetadata.SimpleDisplayText
}
else
{
<table cellpadding="0" cellspacing="0" border="0">
@foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm)))
{
if (prop.HideSurroundingHtml)
{
@Html.Editor(prop.PropertyName)
}
else
{
<tr>
<td>
<div class="editor-label" style="text-align: right;">
@(prop.IsRequired ? "*" : "")
@Html.Label(prop.PropertyName)
</div>
</td>
<td>
<div class="editor-field">
@Html.Editor(prop.PropertyName)
@Html.ValidationMessage(prop.PropertyName, "*")
</div>
</td>
</tr>
}
}
</table>
}
Are you asking to upgrade to the razor syntax? 您是否要升级到razor语法? Otherwise it should still work in mvc 3. Just place your the code in his example in Views/Shared/EditorTemplates/Object.ascx
否则它仍然可以在mvc 3中工作。只需将代码放在他的示例中的Views / Shared / EditorTemplates / Object.ascx中
Lets say you have a view model with a property that returns a list of objects such as 假设您有一个视图模型,该模型的属性返回一个对象列表,例如
public class Product
{
public int ProductId { get; set; }
public string Description { get; set; }
public List<Detail> Details { get; set; }
}
you then wish to generate a view that uses this model. 然后,您希望生成一个使用此模型的视图。 Here is your action method
这是您的操作方法
public ViewResult Edit(int productId)
{
Product product = contextDB.Products.FirstOrDefault(p => p.ProductId == productId);
return View("Edit", product);
}
The generated code will look something like this (incomplete) 生成的代码看起来像这样(不完整)
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>MyViewModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ProductId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProductId)
@Html.ValidationMessageFor(model => model.ProductId)
</div>
...
<fieldset>
}
The generated code will NOT include any code for the List property by default. 默认情况下,生成的代码将不包含List属性的任何代码。 The Razor view engine code generator only goes as deep as the properties of the model.
Razor视图引擎代码生成器仅深入到模型的属性。 You can write code that accesses the Details list in the view but it must be custom code.
您可以编写代码来访问视图中的“详细信息”列表,但是它必须是自定义代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.