I'm attempting to write an html helper in which I want to iterate over a known typed property which is a generic list, and output the required HTML content. Here is an outline of how I have constructed the view model to ensure any view models comply with the helper;
Interface definition:
public Interface ITheViewModelInterface<T> where T : class
{
IEnumerable<T> Items { get; set; }
}
Concrete abstract interface implementation (so that I can define default values) :
public abstract class TheConcreteViewModelImplementation<T> : IViewModelInterface<T> where T : class
{
public virtual IEnumerable<T> Items { get; set; }
}
ViewModel:
public class TheViewModel : TheConcreteViewModelImplementation<MyListType>
{
}
What would be the helper definition such that I could access the correctly typed list of items in the base class? I'm not entirely sure I need a strongly typed helper in this instance, but how do I explicitly pass the items type in the helper call?
MyListType.cshtml
) withinViews\Shared\DisplayTemplates
and then callHtml.DisplayFor(m => m.Items)
to render your items out. – levelnisHtml.DisplayForModel()
and create templates (or use the default ones in the framework) for each data type contained within your model. Have a look at part 4 of Brad's series - it gives a good explanation of customising the default object template – levelnis