Domanda

I usually make use of the Editor and Display templates when my model (or submodels) are List<T> objects. This way I avoid to "spoil" my code with several foreach constructs.

I wanted to know whether there is any way to use Editor and Display templates when the model is a Dictionary<K,V> object instead.

Thanks

Francesco

PS: answers with Razor are more appreciated

È stato utile?

Soluzione

Editor/Display templates don't work well with Dictionary<TKey, TValue>. Personally I avoid using this type as such especially with EditorTemplates as producing correct field names for the model binder could be a PITA.

I would recommend you defining a custom view model:

public class Item
{
    public string Key { get; set; }
    public string Value { get; set; }
}

and then have a IEnumerable<Item> property on your view model for which you define Editor/Display templates. It will make your life much easier. Then all that's left would be to map between your model property (which potentially could be a Dictionary<TKey, TValue>) to the view model property.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top