开发者

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 performa开发者_如何学编程nce difference or other consideration or should i expect the same response time?


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! :-]


If you are converting, at the view end, from ViewData back to your dictionary then yeah there will be a performance hit.

Also, you are now introducing code into the view. Views should have little to no code.

Your View can inherit from a class for a reason. use it. ViewData should only be used for once of little strings like mayby page titles or something.

I'm just fixing up a project here where no view inherits from a model and instead uses ViewData for Lists and the like. Sometimes 15 of them in a single view. The damn thing is so slow but by replacing ViewData with models we are clawing back the speed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜