繁体   English   中英

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

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

我有以下问题:

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

(其中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

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

我如何找出框架选择了哪个模板? 在Visual Studio 2013中使用调用堆栈无济于事,因为我仅获得了将具有模板名称(System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate)的方法的反汇编代码。

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

然后在@Html.DisplayFor(m => m.PkID)上添加一个断点。 它可能会涉及很多其他内容,但最终会出现在模板中

对我来说,确定执行哪个模板的最佳方法是了解MVC框架中显示和编辑器模板的处理方式。 您应该检查几个不同的选项

  • 您在DisplayFor调用中显式指定了模板名称(在您的情况下不是这样)
  • 您在PkID上使用了UIHintAttribute来指示框架使用哪个模板
  • 您在Views / ControllerName / DisplayTemplates下有模板
  • 您已将模板放在“视图/共享/显示模板”下

请注意,即使将模板放置在多边位置上但不包含在Visual Studio解决方案中,它仍将被呈现。

暂无
暂无

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

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