Sitecore 为什么玻璃映射器返回空值?

Sitecore 为什么玻璃映射器返回空值?,sitecore,glass-mapper,sitecore-mvc,Sitecore,Glass Mapper,Sitecore Mvc,我用的是玻璃V4。我有一个MVC Web区域项目的设置 我已经在主项目(WebProject)中安装了玻璃贴图器 我正试图在我的区域项目中进行玻璃浇铸 公共类ContactController:SitecoreController { 私有只读ISitecoreContext\u上下文; 私有IGlassHtml glassHtml; 公共通讯控制器() :此(新SitecoreContext()) { } 公共联系人控制器(ISitecoreContext上下文) { _上下文=上下文; _g

我用的是玻璃V4。我有一个MVC Web区域项目的设置

我已经在主项目(WebProject)中安装了玻璃贴图器

我正试图在我的区域项目中进行玻璃浇铸

公共类ContactController:SitecoreController
{
私有只读ISitecoreContext\u上下文;
私有IGlassHtml glassHtml;
公共通讯控制器()
:此(新SitecoreContext())
{
}
公共联系人控制器(ISitecoreContext上下文)
{
_上下文=上下文;
_glassHtml=新的glassHtml(上下文);
}
//获取:联系
公共行动结果联系人()
{
var db=Sitecore.Context.Database;
var datasource=db.GetItem(RenderingContext.Current.Rendering.datasource);
var ViewModel=new Models.ContactUs();
ViewModel.Headerstring=datasource.Fields[“Headerstring”].Value;
ViewModel.Substring=datasource.Fields[“Substring”].Value;
ViewModel.Description=((MultilistField)datasource.Fields[“Description”]).GetItems().Select(s=>s.Fields[“Line”].Value).ToList();
返回视图(ViewModel);
}
公共行动结果ContactUsGlass()
{
var模型=_context.GetCurrentItem();
返回视图(模型);
}
}
我可以用第一个动作方法得到值,但不能用第二个动作方法

型号:

公共类联系人
{
公共字符串头字符串{get;set;}
公共字符串子字符串{get;set;}
公共列表说明{get;set;}
}
玻璃型号:

公共类ContactUsGlassModel
{
公共虚拟字符串头字符串{get;set;}
公共虚拟字符串子字符串{get;set;}
}

我知道我不需要在Glass V4中注册我的命名空间

您不应该使用
\u context.GetCurrentItem
方法。改用
\u context.GetItem

public ActionResult ContactUsGlass()
{
var model=context.GetItem(RenderingContext.Current.Rendering.DataSource);
返回视图(模型);
}

您不想从
Sitecore.Context.Item
(在
GetCurrentItem
方法中使用。您希望从当前渲染的数据源获取模型。

@Marek所回答的是将渲染项拉入模型的正确方法。默认情况下,GetCurrentItem提供Sitecore服务的页面项。如果您的模型需要的字段是页面的字段然后GetCurrentItem也可以填充您的模型。如果启用了数据源嵌套,则如果未在呈现上设置数据源,Sitecore将再次返回页面项。

您可以从GlassController继承,然后使用GetLayoutItem()获取datasorced项。如果为空,则需要在sitecore中发布模板,并确保映射正确(如果未使用TDS:)

是否可以检查项目是否已发布,以及是否有上下文语言版本?@SiteCoreClumper,是的。我已更新代码,var model=\u context.GetItem(RenderingContext.Current.Rendering.DataSource);但现在由于RenderingContext的原因,我无法对代码进行单元测试。请您对此提出建议。@Sakthivel值得一读@Sakthivel,您可以从RenderingContext类中提取一个IRenderingContext接口。为IRenderingContext创建一个MVC模型绑定器并注册它。我通常对大多数Sitecore都使用此模式。Context re相关属性。同样值得注意的是GlassModelBinder的这种模式。虽然不是同样的方式,但我在解决方案中使用了类似的方法。我当然也会尝试这种方法…谢谢。。。!