简体   繁体   English

MVC DisplayFor函数-如何找出使用哪个模板?

[英]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: (其中PkID的类型为long)失败,但出现以下异常:

[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. 我们的项目中有很多DisplayTemplates和EditorTemplates,我敢肯定使用了其中的一个而不是默认的模板,但是我需要知道(错误地)选择了哪个模板,以便修复内容。

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). 在Visual Studio 2013中使用调用堆栈无济于事,因为我仅获得了将具有模板名称(System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate)的方法的反汇编代码。

In Visual Studio, go to Tools > Options > Debugging and uncheck Step over properties and operators (Managed Only) 在Visual Studio中,转到“ Tools > Options > Debugging然后取消选中“ Step over properties and operators (Managed Only)

Then add a breakpoint on @Html.DisplayFor(m => m.PkID) . 然后在@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. 对我来说,确定执行哪个模板的最佳方法是了解MVC框架中显示和编辑器模板的处理方式。 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) 您在DisplayFor调用中显式指定了模板名称(在您的情况下不是这样)
  • You used UIHintAttribute on PkID to instruct framework which template to use 您在PkID上使用了UIHintAttribute来指示框架使用哪个模板
  • You have template under Views/ControllerName/DisplayTemplates 您在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. 请注意,即使将模板放置在多边位置上但不包含在Visual Studio解决方案中,它仍将被呈现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在MVC 3中编辑Html.DisplayFor方法的CSS? - How do I edit the CSS of the Html.DisplayFor method in MVC 3? 如何格式化要在DisplayFor中使用的MVC模型类中的字符串属性 - How to format string property in MVC model class to be used in DisplayFor 如何将DisplayFor()用于不直接属于asp .net mvc 2模型中模型的对象? - How do I use DisplayFor() for objects that are not directly part of my model in asp .net mvc 2? 如何确定运行时是否使用了枚举值? - How do I find out if an enum value is used at Runtime? 如何找出PDF中的图像? - How do I find out which images are where in a PDF? 如何找出哪个按钮触发了回发? - How do I find out which button triggered the postback? 我如何找出哪些列的值无效 - How do I find out, which columns has invalid value 为什么ASP.NET MVC模板视图使用Html.DisplayFor显示不属于模型的数据? - Why do the ASP.NET MVC template views use Html.DisplayFor to display data that is not part of the model? 我如何找出用于签署PKCS7消息的证书? - How can I find out which certificate was used to sign a PKCS7 message? 在这种情况下,如何使Html.DisplayFor工作? - How do I make Html.DisplayFor work in this case?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM