[英]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 您应该检查几个不同的选项
PkID
to instruct framework which template to use 您在PkID
上使用了UIHintAttribute来指示框架使用哪个模板 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.