未保存字段值更改
我试图从后端动态更改字段值,但看起来像没有保存更改。未保存字段值更改
代码
item
是从主数据库提取的。
using (new EditContext(item))
{
item.Editing.BeginEdit();
try
{
//Value is updated here from "" to Test
item.Fields["Content"].Value = "Test";
}
finally
{
//item.Fields["Content"].Value is "" again.
item.Editing.AcceptChanges();
item.Editing.EndEdit();
}
}
UPDATE
正如@sitecore登山者说,我没有改变我的代码回用 -
然而,问题是缓存。只有在清除缓存并重新启动浏览器之后,更新后的值才显示在内容编辑器中。
为了解决这个问题,我在编辑之前禁用了缓存,并在编辑完成后将其重新打开。
CacheManager.Enabled = false;
using (new Sitecore.SecurityModel.SecurityDisabler())
{
item.Editing.BeginEdit();
try
{
item.Fields["Content"].Value = "Test";
}
finally
{
item.Editing.EndEdit();
}
}
CacheManager.Enabled = true;
请加:(新Sitecore.SecurityModel.SecurityDisabler())
编辑上下文containts代码下一行:
public EditContext(Item item)
{
Assert.ArgumentNotNull((object) item, "item");
this._item = item;
this._item.Editing.BeginEdit();
}
,所以如果你在有你并不需要你在这里代码 item.Editing.BeginEdit();
你的代码必须是:
using (new Sitecore.SecurityModel.SecurityDisabler())
{
item.Editing.BeginEdit();
try
{
//Value is updated here from "" to Test
item.Fields["Content"].Value = "Test";
}
finally
{
//item.Fields["Content"].Value is "" again.
// Remove AcceptChanges I never use it , for editing .
// item.Editing.AcceptChanges();
item.Editing.EndEdit();
}
}
我我的答案更新,你的内容编辑器检查是否有什么变化? 您可以清除缓存,然后再次检查。这真的很奇怪,为什么不工作,我猜可能是一个缓存问题。
我忘了补充一点,我也曾尝试过。而且,它也没有工作。它失去了AcceptChanges()或EndEdit()的值。 – NomadTraveler
我更新了我的答案。 – 2013-10-28 06:47:34
你说得对,关于缓存。当我清除缓存并重新启动网站时,我可以在内容编辑器中看到更新后的值。任何想法如何强制值在CE中更新而不清除缓存? – NomadTraveler
使用SecurityDisabler如果它有助于尝试..
using (new Sitecore.SecurityModel.SecurityDisabler())
{
item.Editing.BeginEdit();
try
{
//Value is updated here from "" to Test
item.Fields["Content"].Value = "Test";
}
finally
{
//item.Fields["Content"].Value is "" again.
item.Editing.AcceptChanges();
item.Editing.EndEdit();
}
}
是变化致力于你所期望的数据库?尝试切换到Web/Master以查看更改是否存在。 –
我正在尝试仅对主数据库进行更改。切换也不起作用。 – NomadTraveler
您是否试图将所有内容放在try {}中,或者在没有try/finally全部的情况下进行测试?也许这些示波器搞砸了。 –