簡體   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