Model view controller Sitecore MVC字段以编程方式更新

Model view controller Sitecore MVC字段以编程方式更新,model-view-controller,sitecore,sitecore7,Model View Controller,Sitecore,Sitecore7,如何使用MVC更改字段值 我在视图中有这样一个表单,并在ViewRendering项中定义了控件名称和操作 @使用(Html.BeginRouteForm(Sitecore.Mvc.Configuration.MvcSettings.SitecoreRouteName,System.Web.Mvc.FormMethod.Post)) { @Html.Sitecore().FormHandler(“组件”、“测试控制器”) @DropDownListFor(model=>model.TypeLis

如何使用MVC更改字段值

我在视图中有这样一个表单,并在ViewRendering项中定义了控件名称和操作

@使用(Html.BeginRouteForm(Sitecore.Mvc.Configuration.MvcSettings.SitecoreRouteName,System.Web.Mvc.FormMethod.Post))
{
@Html.Sitecore().FormHandler(“组件”、“测试控制器”)
@DropDownListFor(model=>model.TypeList,新列表
{ 
新建SelectListItem{Text=“Enable”,Value=“True”,Selected=(isSet?True:false)},
新建SelectListItem{Text=“Disable”,Value=“False”,Selected=(!isSet?true:False)}
})
}
我想做的是,当单击“提交”时,controller将以EditorMode获取表单数据并更新Sitecore中的字段值

我该怎么办

在控制器中,我的想法是:

公共类组件:SitecoreController
{
公共操作结果testController()
{
如果(提交点击次数){
ComponentModel ob=新的ComponentModel();
ob.Initialize(RenderingContext.Current.Rendering);
string selectedValue=视图中的selectedValue;
ob.item.Editing.BeginEdit();
使用(新的EditContext())
{
ob.CheckBoxField.Checked=(selectedValue=“真”?真:假);
}
ob.item.Editing.EndEdit();
返回局部视图(ob);
}
返回PartialView();
}
}    

尝试将其包装在SecurityDisabler中。伪代码:

public ActionResult testController()
{
如果(提交点击次数){
ComponentModel ob=新的ComponentModel();
ob.Initialize(RenderingContext.Current.Rendering);
string selectedValue=视图中的selectedValue;
使用(新的SecurityDisabler()){
var item=Sitecore.Data.Databases.GetDatabase(“主”);
item.Editing.BeginEdit();
item.Fields[“CheckBoxField”]。Value=(selectedValue==“True”?True:false);
item.Editing.EndEdit();
item.SaveChanges();
}
返回局部视图(ob);
}
返回PartialView();
}

问题已经解决,通过使用Ajax的控制器渲染,我完全得到了想要做的事情

对于复选框字段类型,我使用了DropList而不是Checkbox

型号

公共类组件模型:IRenderingModel
{
公共字符串标题{get;set;}
公共项项{get;set;}
公共项PageItem{get;set;}
public Sitecore.Data.Fields.CheckboxField chBox{get;set;}
…基于模板的其他一些声明的数据类型,如果需要。。。
公共void初始化(Sitecore.Mvc.Presentation.Rendering)
{
渲染=渲染;
Item=渲染。Item;
PageItem=Sitecore.Mvc.Presentation.PageContext.Current.Item;
Title=FieldRenderer.Render(项目“Title”);
…如果你想的话就多点。。。
}
}
控制器

公共类组件:控制器
{
//
//POST:/组件/
公共操作结果组件视图(字符串更改值、字符串字段名)
{
ComponentModel ss=新的ComponentModel();
初始化(RenderingContext.Current.Rendering);
项目=ss.项目;
如果(!String.IsNullOrEmpty(changedValue)和&!String.IsNullOrEmpty(fieldName))
{
ss.Item.Editing.BeginEdit();
使用(新的SecurityDisabler())
{
交换机(字段名)
{
条件
}
}
ss.Item.Editing.EndEdit();
}
返回部分视图(ss);
}
}
查看

@model yournamespace.ComponentModel
@使用Sitecore.Mvc
@if(Sitecore.Context.PageMode.IsPageEditor)
{  
if(!@Sitecore.Data.ID.IsID(Model.Rendering.DataSource))
{
没有关联的数据源。
请创建新的数据源
} 其他的 { 这是页面编辑器 标题:@Html.Raw(Model.Title)
投递员: 使可能 使残废
函数dlOnChangeUpdate(字段名) { $(“#”+字段名).on('change',function(){ var changedValue=$(“#”+fieldName).val(); $.ajax({ url:'@url.Action(“ComponentModel”、“Components”), 类型:“POST”, 数据:{“changedValue”:changedValue,“fieldName”:fieldName}, 背景:这,, 成功:功能(数据){ Sitecore.PageModes.PageEditor.postRequest('webedit:save()'); console.log(“成功”,数据); }, 错误:函数(数据){ 警报(“错误:”+数据); console.log(“错误”,数据); } }); }); } } } 其他的 { 这是预览 }
您能澄清一下编辑器模式下的含义吗?你的意思是让这段代码在页面编辑器中工作吗?对不起,问题不清楚。是的。代码在页面编辑器模式下工作。但是,这不是主要问题。我可以很容易地添加“Sitecore.Context.PageMode.IsPageEditorEditing”这样的条件,我也尝试过,但这不是主要问题。我想知道的是“如果(提交点击)”部分