How do I obtain an HtmlHelper<TModel> instance for a model in ASP.NET MVC?

Posted by DanM on Stack Overflow See other posts from Stack Overflow or by DanM
Published on 2010-03-29T20:50:25Z Indexed on 2010/03/29 20:53 UTC
Read the original article Hit count: 150

Filed under:
|

Let's say I have an Index view. The model I pass in is actually a collection of models, so the Html property is of type HtmlHelper<List<MyModel>>. If I want to call extension methods (e.g., Display() or DisplayFor() on the individual items in the list, however, I think I need to obtain an HtmlHelper<MyModel>. But how?

I tried using the HtmlHelper<TModel> constructor, which looks like this:

HtmlHelper<TModel>(ViewContext, IViewDataContainer)

But I'm not having any luck with that. I don't know how to obtain the IViewDataContainer for the item, and the documentation on these things is very sparse.

A lot of magic apparently happens when I do...

return View(List<MyModel>);

...in my controller.

How do I recreate that magic on individual items in a list/collection?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about c#