GlassMapper 数据源和当前项目

GlassMapper Datasource and current item

我正在使用从玻璃映射器到 return 我的数据源项目到视图的 GetDataSourceItem 方法,我在数据源为空时调试代码,控制器中的 calloutModel 将为 null,但从视图来看,模型将为不为空,它将具有我正在使用以下代码的当前项目模型:

我的控制器动作:

 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)

看起来这是由于 GlassView 基础 class。 class 覆盖 InitHelpers 方法并在模型为 null 时调用其 GetModel 方法。如果没有数据源项,GetModel 方法将回退到上下文项。

为防止这种情况发生,您可以将 @inherits 指令更改为 @model CalloutModel,然后使用 @Html.Glass() 帮助器来访问可编辑方法等。