在ASP.NET MVC3 RC2显示复杂类型 [英] Displaying complex types in ASP.NET MVC3 RC2

查看:105
本文介绍了在ASP.NET MVC3 RC2显示复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用复杂类型作为属性的模型。

I have a model that uses a complex type as a property.

namespace Web.Models {
    public class Business : IModel {
        [Key, HiddenInput(DisplayValue = false)]
        public Guid ID { get; set; }

        public Position Position { get; set; }

        public bool Active { get; set; }

        public ICollection<Comment> Comments { get; set; }

        public Business() {
            ID = Guid.NewGuid();
            Position = new Position();
        }
    }

    public class Position {
        public double Latitude { get; set; }
        public double Longitude { get; set; }
    }
}

当我来到创造商业模式的一种形式,是不显示的位置属性。我敢肯定,复杂类型被默认在MVC2显示。假设这可能​​是在MVC3交换机我尝试以下步骤无济于事:

When I came to create a form of the Business model, the Position property wasn't displayed. I'm sure complex types were displayed by default in MVC2. Assuming that this might be a switch in MVC3 I tried the following steps to no avail:


  1. 装饰用 ScaffoldColumn(真)属性的位置属性。

  2. 在创建视图\\共享\\ EditorTemplates一个Position.cshtml视图。

  1. Decorated the Position property with the ScaffoldColumn(true) attribute.
  2. Created a Position.cshtml view in Views\Shared\EditorTemplates.

我目前的解决方法一直是我的自定义Object.ascx从MVC2转换为MVC3剃刀Object.cshtml。我犹豫的是,我敢肯定我的自定义Object.ascx是基于作为博客布拉德·威尔逊原。

My current workaround has been to convert my custom Object.ascx from MVC2 to an MVC3 Razor Object.cshtml. My hesitation is that I'm positive my custom Object.ascx was based on the original as blogged by Brad Wilson.

@if (ViewData.TemplateInfo.TemplateDepth > 1) {
    @ViewData.ModelMetadata.SimpleDisplayText
}
else {
    <div class="editor">
        @foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm))) {
      if (prop.HideSurroundingHtml) {
            @Html.Editor(prop.PropertyName)
      }
      else {
            <div class="field">
                @(prop.IsRequired ? "*" : "")
                @Html.Label(prop.PropertyName)
                @Html.Editor(prop.PropertyName)
                @Html.ValidationMessage(prop.PropertyName)
            </div>
      }
  }
    </div>
}

所以,问题是:


  1. 的默认行为改变还是我失去了一些东西?

  2. 是否有更好的方法来对复杂类型的可见性切换?

  3. 是否有可能访问默认模板在ASP.NET MVC3?

推荐答案

默认Object.ascx模板将只显示一个对象图的一个级别。

The default Object.ascx template will only display one level of an object graph.

有位于上部的一行checkes以查看是否深度> 1,然后捞出上渲染。

There is a line on top that checkes to see if the depth is > 1 and then bails on rendering.

<% } else if (ViewData.TemplateInfo.TemplateDepth > 1) { %>

更改为:

<% } else if (ViewData.TemplateInfo.TemplateDepth > 99) { %> 

或删除全部,如果

这篇关于在ASP.NET MVC3 RC2显示复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆