Search code examples
asp.net-mvc-2viewmodelviewdatastrongly-typed-vieweditorfor

Asp MVC 2: Typed Editor Template


(I reference this tutorial in this text)

I want to use the Html.EditorFor (or Html.Editor) helpers.

If a UserControl needs additional data it is passed via

...EditorFor(model => model.Album, new { Artists = Model.Artists, ... })

In the UserControl it's accessed via ViewData[stringKey], ie

... new SelectList(ViewData["Artists"] as IEnumerable, ...

To me this smells a little fishy as I would prefer a strongly typed ViewModel which ensures that specific data is available.

I'm now a little bit stuck as I don't know wheater there's a "typed way" to find or I should accept this way as-is.

How did you solve this issue? Any help appreciated!

Lg
warappa


Solution

  • I would probably change my view model so that I don't need to pass this additional information. You could make for example an album has a collection of artists. Now all tha you will have to do is:

    <%: Html.EditorFor(model => model.Album) %>
    

    And in your editor template:

    <%: Html.DropDownListFor(x => x.SelectedArtist, new SelectList(Model.Artists)) %>