我正在使用带有MVC的sitecore 8.1,我需要一个单行文本&带有占位符文本的营销人员的Web表单中的电子邮件字段。 我已成功创建了一个带占位符的自定义文本字段,但有一个小问题,即它不是共享字段或支持多文化。 我跟随@ azadeh-khojandi回复了mvc here
我有一个最后的解决方案,可以在占位符中获取字典键,并从代码获取键值,这不应该是一个好主意。 任何提示或指南?
类:
[ValidationProperty("Text")]
public class SingleLineText : Sitecore.Form.Web.UI.Controls.SingleLineText, IPlaceholderField
{
[VisualCategory("Custom Properties")]
[VisualProperty("Placeholder", 2)]
[DefaultValue("")]
public string PlaceHolder { get; set; }
protected override void OnInit(EventArgs e)
{
// Set placeholder text, if present
if (!string.IsNullOrEmpty(PlaceHolder))
{
textbox.Attributes["placeholder"] = Helper.GetDictionaryItem(PlaceHolder);
}
base.OnInit(e);
}
}
public class ExtendedSingleLineTextField : Sitecore.Forms.Mvc.ViewModels.Fields.SingleLineTextField, IPlaceholderField
{
[VisualCategory("Custom Properties")]
[VisualProperty("Placeholder", 2)]
[DefaultValue("")]
public string PlaceHolder { get; set; }
}
public interface IPlaceholderField
{
string PlaceHolder { get; set; }
}
public static class BootstrapEditorHtmlHelperExtension
{
public static MvcHtmlString ExtendedBootstrapEditor(this HtmlHelper helper, string expression, string placeholderText, string inlineStyle, string[] classes)
{
var str = string.Empty;
var viewModel = helper.ViewData.Model as IViewModel;
if (viewModel != null)
{
var styleSettings = viewModel as IStyleSettings;
if (styleSettings != null)
{
str = styleSettings.CssClass;
}
if (string.IsNullOrEmpty(placeholderText))
{
placeholderText = viewModel.Title;
}
}
return helper.Editor(expression, new
{
htmlAttributes = new
{
@class = (string.Join(" ", classes) + " form-control" + (string.IsNullOrEmpty(str) ? string.Empty : " " + str) + (helper.ViewData.Model is SingleLineTextField ? " dangerousSymbolsCheck" : string.Empty)),
placeholder = placeholderText,
style = (inlineStyle ?? string.Empty)
}
});
}
}
查看自定义字段:
@using (Html.BeginField())
{
@Html.ExtendedBootstrapEditor("value", Model.PlaceHolder, "", new[] { "" })
}
答案 0 :(得分:1)
我已通过在“ ExtendedSingleLineTextField ”和“ SingleLineText ”类中为PLaceholder添加[Localize]
属性来解决此问题。
这是在Sitecore.Form.Core.Attributes.LocalizeAttribute
定义的,也是进一步定制参考。