繁体   English   中英

如何在自动生成的视图中使ASP.NET MVC 3呈现具有复杂类型的对象?

[英]How do I make ASP.NET MVC 3 render objects with complex types in the auto-generated views?

我有一些模型类,它们具有复杂类型的属性项(即其他模型类)。 当我从Visual Studio自动生成视图时,如何使这些类(包含在顶级类中)正确显示?

基本上,如何将http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html更新为ASP.NET MVC 3?

TIA,
本杰

来吧,自己做些努力,说出遇到的困难! 否则,您期望如何学习?

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>
}

您是否要升级到razor语法? 否则它仍然可以在mvc 3中工作。只需将代码放在他的示例中的Views / Shared / EditorTemplates / Object.ascx中

假设您有一个视图模型,该模型的属性返回一个对象列表,例如

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视图引擎代码生成器仅深入到模型的属性。 您可以编写代码来访问视图中的“详细信息”列表,但是它必须是自定义代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM