如何在自动生成的视图中使用复杂类型的ASP.NET MVC 3渲染对象?

时间:2021-02-09 03:27:42

I have model classes that have as properties items of complex types (i.e., 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,
Benjy

3 个解决方案

#1


1  

Are you asking to upgrade to the razor syntax? 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中的示例中。

#2


4  

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:

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

#3


1  

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. The Razor view engine code generator only goes as deep as the properties of the model. You can write code that accesses the Details list in the view but it must be custom code.

默认情况下,生成的代码不包含List属性的任何代码。 Razor视图引擎代码生成器仅与模型的属性一样深。您可以编写访问视图中“详细信息”列表的代码,但必须是自定义代码。

#1


1  

Are you asking to upgrade to the razor syntax? 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中的示例中。

#2


4  

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:

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

#3


1  

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. The Razor view engine code generator only goes as deep as the properties of the model. You can write code that accesses the Details list in the view but it must be custom code.

默认情况下,生成的代码不包含List属性的任何代码。 Razor视图引擎代码生成器仅与模型的属性一样深。您可以编写访问视图中“详细信息”列表的代码,但必须是自定义代码。