简体   繁体   中英

MVC DisplayFor function - how do I find out which template is used?

I have the following problem:

@Html.DisplayFor(m => m.PkID)

(where PkID is of type long) fails with the following exception:

[InvalidOperationException: The model item passed into the dictionary is of type 'System.Int64', but this dictionary requires a model item of type 'System.String'.]
   System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +378
   System.Web.Mvc.ViewDataDictionary.set_Model(Object value) +47
   System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +614
   System.Web.Mvc.ViewDataDictionary`1..ctor(ViewDataDictionary viewDataDictionary) +37
   System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) +98
   System.Web.Mvc.WebViewPage.set_ViewData(ViewDataDictionary value) +39
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +425
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +382
   System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, String templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames, GetDefaultActionsDelegate getDefaultActions) +1037
   System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate) +1621
   System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData) +94
   System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData, TemplateHelperDelegate templateHelper) +225
   System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData) +140
   System.Web.Mvc.Html.DisplayExtensions.DisplayFor(HtmlHelper`1 html, Expression`1 expression) +92

We have lots of DisplayTemplates and EditorTemplates in our project and I am certain that one of them is used instead of the default, but I need to know which template is chosen (incorrectly) so that I can fix the content.

How do I find out which template was chosen by the framework? Using the call stack in Visual Studio 2013 didn't help because I only get decompiled Assembler code for the method that would have the template name (System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate).

In Visual Studio, go to Tools > Options > Debugging and uncheck Step over properties and operators (Managed Only)

Then add a breakpoint on @Html.DisplayFor(m => m.PkID) . It will probably step into lots of other stuff but will eventually end up in the template

For me, the best way to determine which template is executed is to know how display and editor templates are handled in MVC framework. There are just a few different options you should check

  • You explicitly specified template name in DisplayFor call (which is not the option in your case)
  • You used UIHintAttribute on PkID to instruct framework which template to use
  • You have template under Views/ControllerName/DisplayTemplates
  • You have template placed under Views/Shared/DisplayTemplates

Note that even if template is placed on plysical location but excluded from visual studio solution, it will still be rendered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM