mercredi 11 mai 2016

Displaying Complex Object in MVC View using Reflection

I have a Strongly Typed object that has nested properties as well as lists, I have simplified it here.

public class Person
{
     public string Name {get; set;}
     public Address Address {get; set;}
}

public class Address
{
     public List<string> AddressList {get; set;}
     public string City {get; set;}
     public string State {get; set;}
}

I want to be able to edit an instance of this object on the UI of my MVC site. I have this snippet in my View that works for flat objects.

@foreach (var property in ViewData.ModelMetadata.Properties)
{
    <div class="editor-line">
        <label>@(property.DisplayName ?? property.PropertyName)</label>
        @Html.Editor(property.PropertyName)
    </div>
}

But I need to be able to traverse an object and display all indices of a property that is an array or all properties if the property is a class. Is there a simple way to do this?





Aucun commentaire:

Enregistrer un commentaire