Find: DisplayTemplates Speed

ⅰ亾dé卋堺 提交于 2019-12-05 08:40:38

I had EXACTLY the same issue... after some searching around I found I was using:

@DisplayFor(x => x.StringProperty);

After thinking about it, and finding out about how all the DisplayFor/EditorFor methods work from making some templates myself, it made no sense.

(A bit of explanation on how DisplayFor/EditorFor works)

When using DisplayFor/Editor for, MVC gets the type of object and then searches the Views/ControllerName/DisplayTemplates directory for a view with the same name as that type, in this case, it's searching for Views/ControllerName/DisplayTemplates/String.cshtml. As it doesn't exist, it also does the same in the Shared/DisplayTemplates views directory, again, it won't exist.

(This next bit is speculation)

I would presume that as it can't find the relevant Display/Editor template, it then performs a ToString() on the object, as a fail over.

As you are only displaying a String type anyway, it would make sense to not use the DisplayFor(x => StringProperty) and just use @Model.StringProperty, which would not cause MVC to search for a DisplayTemplate, and just render it as a string, which it's going to do anyway.

Joey Gennari

Once I put a profiling block around the bundles in the <head> I could see where the time was really being spent. Mini-profiler was misleading me, originally: the time was not spent in DisplayTemplates/String but elsewhere!

In my case, the delay was happening in MVC4 RC's script bundling.

I removed the bundles and all is good.

See related issue below:

MVC4 RC script bundling very slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!