views:

91

answers:

2

When I use the ViewData.ModelMetadata.Properties in order to loop the properties (with an additional Where(modelMetadata => modelMetadata.ShowForEdit && !ViewData.TemplateInfo.Visited(modelMetadata))), and thereby create a generic edit view, the properties of the view model's base class are rendered last.

Is it possible to use a clever OrderBy() or is there another way to first get the properties of the base class, and then the sub class'?

Reverse won't do the trick since the ordering of each class' properties is perfectly fine.

A workaround would of course be composition + delegation, but since we don't have mixins, it's too un-DRY IMHO, why I seek a better solution if possible.

A: 

Looking at ViewData.ModelMetadata.Properties I dont see any information that shows if a property is part of the Model class or of its Superclass.

One way would be to put an attribute on the properties of the Superclass. If this is a attribute "DisplayMeAtTheTop" it could be used by other properties too, that have to be on top.

Malcolm Frexner
Before asking my question, I found a blog post (http://blog.maartenballiauw.be/post/2010/01/11/Ordering-fields-in-ASPNET-MVC-2-templated-helpers.aspx) where the author created an Order(int) attribute in order to control the ordering.I also investigated the properties of ModelMetadata but couldn't find anything useful for an OrderBy, but was hoping I had overlooked something.I was hoping to find a simpler solution to my problem than a custom attributes, but it's at least one way to solve the problem.
Martin R-L
Interesting post. This ordering thing should be in the default MetaDataProvider. Its pretty much essential.
Malcolm Frexner
A: 

I went with another solution that doesn't use inheritance.

The case is; based on a type, only a sub-set of the properties should be editable.

I solved it by defining a set of property lambda expressions for each type.

Martin R-L