4

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

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

当我开始创建业务模型的表单时,没有显示 Position 属性。我确信在 MVC2 中默认显示复杂类型。假设这可能是 MVC3 中的一个开关,我尝试了以下步骤但无济于事:

  1. 用属性装饰了 PositionScaffoldColumn(true)属性。
  2. 在 Views\Shared\EditorTemplates 中创建了一个 Position.cshtml 视图。

我当前的解决方法是将我的自定义 Object.ascx 从 MVC2 转换为 MVC3 Razor Object.cshtml。我的犹豫是,我很确定我的自定义 Object.ascx 是基于 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 中的默认模板?

富有的

4

1 回答 1

0

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

顶部有一条线检查深度是否 > 1,然后在渲染时退出。

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

改成:

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

或者如果完全删除它。

于 2010-12-15T13:03:02.723 回答