Search code examples
asp.net-mvcviewmodelviewdata

is there a performance different between passing something into viewdata compared to a view model


in asp.net mvc , if i shove a dictionary or an array of objects in ViewData and read that in my view compared to creating a view model class that has that same data structure, is there a performance difference or other consideration or should i expect the same response time?


Solution

  • The response time would be minimal and wouldn't really make a significant impact on the performance.
    It even decreases your performance and is very hacky because you'll have to make extensive use of boxing and unboxing which will fail eventually.

    Yet the View Model is the appropriate way to provide model data to the view because it gives you so many more possibilities to work with:

    It provides:

    • Saver usage of ViewModels
    • Easy model validation (even the one with Html.EnableClientSideValidation())
    • Strongly typed Views (you'll achieve this after unboxing..)
    • Auto generated View which don't need to be adjusted (awesome when you're using T4 templates) -> it will save you so much time when you do adjustments to your model..

    And on top of that. The entire framework is built on top of these little gadget which support you on your journey.

    Now go and break some code! :-]