本文介绍了创建项目时的Sitecore WFFM复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Web表单中使用"创建项目"保存操作。我有一个复选框列表,指向包含类别项目的文件夹。这将映射到它将在用户提交表单时创建的项模板上的多列表域。但是它传递的是复选框文本,而不是值,因此创建的每个新项的多列表中都有错误数据。有人知道如何将复选框列表设置为传递这些值吗?我有点惊讶它一开始就会这么做。
推荐答案
我还没有机会对此进行测试,但理论上,您可以创建一个继承自CheckboxList
的新字段类型,我们将其命名为CheckboxListPipedValues
。代码如下所示:
using System.ComponentModel;
using System.Linq;
using System.Text;
using Sitecore.Form.Core.Controls.Data;
using Sitecore.Form.Web.UI.Controls;
public class CheckboxListPipedValues : Sitecore.Form.Web.UI.Controls.CheckboxList
{
[Browsable(false)]
public override ControlResult Result
{
get
{
StringBuilder stringBuilder1 = new StringBuilder();
var checkedItems = this.InnerListControl.Items.Where(a => a.Selected).ToList();
var values = string.Join("|", checkedItems.Select(c => c.Value));
foreach (var item in checkedItems)
{
stringBuilder1.AppendFormat("{0}, ", item.Text);
}
return new ControlResult(this.ControlName, values, stringBuilder1.ToString(0, (stringBuilder1.Length > 0 ? stringBuilder1.Length - 2 : 0)));
}
}
}
在Sitecore中,只需转到并复制该项目。然后将程序集和类更改为新控件。更改表单以使用新字段,并确保值映射正确。现在,值的输出应该是一个用竖线分隔的值列表,它应该可以很好地与多列表字段配合使用。编辑:对于MVC,这是相同的过程,但是您需要更新字段类型Item中的MVC Type
以指向您的新类。MVC的代码应该如下所示:
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Form.Core.Controls.Data;
using Sitecore.Forms.Core.Data;
using Sitecore.Forms.Mvc.Data.TypeConverters;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Web.Mvc;
public class CheckBoxListPipedField : CheckBoxListField
{
public CheckBoxListPipedField(Item item) : base(item)
{
}
public override ControlResult GetResult()
{
var values = new List<string>();
StringBuilder stringBuilder1 = new StringBuilder();
if (this.Items != null)
{
foreach (SelectListItem selectListItem in
from item in this.Items
where item.Selected
select item)
{
values.Add(selectListItem.Value);
stringBuilder1.AppendFormat("{0}, ", selectListItem.Text);
}
}
var results = string.Join("|", values);
return new ControlResult(base.ID.ToString(), base.Title, results, stringBuilder1.ToString(0, (stringBuilder1.Length > 0 ? stringBuilder1.Length - 2 : 0)));
}
}
这篇关于创建项目时的Sitecore WFFM复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!