Mapping complex types in 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; } } } 

When I came to create a business model form, the Position property was not displayed. I'm sure complex types were displayed by default in MVC2. Assuming this could be a switch in MVC3, I tried to follow these steps: [/ p>

  • Decorated with the Position property with the ScaffoldColumn(true) attribute ScaffoldColumn(true) .
  • Created the View.cshtml view in the \ Shared \ EditorTemplates views.

My current workaround was to convert my custom Object.ascx from MVC2 to MVC3 Razor Object.cshtml. My doubt is that I'm sure my custom Object.ascx was based on the original, written 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> } 

So the questions are:

  • Is the default behavior changed or am I missing something?
  • Is there a better way to enable visibility of complex types?
  • Can I access default templates in ASP.NET MVC3?

Rich

+4
source share
1 answer

By default, the Object.ascx template displays only one level of the object graph.

At the top there is a line that checks if the depth is> 1, and then it is taken when rendering.

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

Change to:

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

Or delete it if completely.

0
source

Source: https://habr.com/ru/post/1332207/


All Articles