2016-03-22 35 views
2

我正在使用從玻璃映射器GetDataSourceItem方法返回我的數據源項目視圖,我調試代碼時,數據源爲空控制器中的calloutModel將爲null,但從視圖模型將不可空,將有目前的項目模型,我用下面的代碼:GlassMapper數據源和當前項目

我的控制器操作:

public ActionResult Callout() 
     { 
      // I didn't fill the datasource in the component 
      // calloutModel value is coming null. 

      var calloutModel= GetDataSourceItem<CalloutModel>(); 
      return View(calloutModel); 
     } 

我看來

@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<CalloutModel> 
// Model is coming the current item in the view (it should be null) 
+0

是否啓用了mvc.getModel管道的Glass處理器? –

+0

是的,我在Glass.Mapper.Sc.config –

+0

中創建了Sitecore模型,位於/ sitecore/layout/Models下?您需要分配給模型字段上的控制器渲染。 –

回答

4

看起來這是由於GlassView的基類。該類重寫InitHelpers方法並在模型爲null時調用其GetModel方法。如果沒有數據源項目,GetModel方法將回退到上下文項目。

爲了防止出現這種情況,您可以將@inherits指令更改爲@model CalloutModel,然後使用@ Html.Glass()幫助程序訪問可編輯方法等。

+0

工作正常,我可以使用可編輯的方法,但是當我使用渲染圖像像「@ Html.Glass()。RenderImage(item,x => x.Image)」我得到錯誤:「該調用是模棱兩可的之間的以下方法或屬性:RenderImage「 –

+0

它現在工作正常,你應該提供整個參數,以避免雄心勃勃的問題:@ Html.Glass()。RenderImage(item,x => x.Image,null,isEditable:true ) –

相關問題