user156888
user156888

Reputation:

Create Expression<Func<TModel, TValue>> using refelction

I have some Display Templates in my ASP.NET MVC project. Right now i can use them like so:

@Model MyModel

@Html.DisplayFor(x => x.PropertyName)

What I'd like to do is just loop through the object graph and do this dynamically:

@Model MyModel

foreach(var property in MyModel.GetType().GetProperties()){
    @Html.DisplayFor(/*Any way to do this?*/)
}

Any ideas?

Upvotes: 1

Views: 870

Answers (2)

John Farrell
John Farrell

Reputation: 24754

If I remember correctly this is exactly what DisplayForModel already does:

@Html.DisplayForModel(yourModel);

Upvotes: 1

user156888
user156888

Reputation:

http://weblogs.asp.net/rashid/archive/2010/02/09/asp-net-mvc-complex-object-modelmetadata-issue.aspx

with thanks to Jeremy Skinner for finding that for me :)

Upvotes: 0

Related Questions