我的模型类具有复杂类型的属性项(即,其他模型类)。当我从Visual Studio自动生成视图时,如何才能正确地显示这些类(包含在顶级类中)?
基本上,我如何将http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html更新到ASP.NET MVC3?
蒂娅
本吉
发布于 2011-11-01 11:55:53
您是否要求升级到razor语法?否则,它仍然可以在MVC3中工作。
发布于 2011-11-01 16:30:01
来吧,自己努力一下,告诉我你遇到了什么困难!否则你怎么能期望学到一些东西呢?
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
@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
@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
@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>
}发布于 2011-12-17 01:11:08
假设您有一个视图模型,该模型具有一个返回对象列表的属性,例如
public class Product
{
public int ProductId { get; set; }
public string Description { get; set; }
public List<Detail> Details { get; set; }
}然后,您希望生成一个使用此模型的视图。以下是您的操作方法
public ViewResult Edit(int productId)
{
Product product = contextDB.Products.FirstOrDefault(p => p.ProductId == productId);
return View("Edit", product);
}生成的代码将如下所示(不完整)
@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>
}默认情况下,生成的代码将不包含List属性的任何代码。Razor视图引擎代码生成器只能深入到模型的属性。您可以编写代码来访问视图中的详细信息列表,但它必须是自定义代码。
https://stackoverflow.com/questions/7962052
复制相似问题