2013-08-28 56 views
0

我正在嘗試在使用剃鬚刀進行渲染的Contour(3.0.14)中創建自定義字段類型。我創建了一個名爲CustomTextfield的新類,並額外添加了Width屬性,然後在~/umbraco/Plugins/umbracoContour/Views中創建了一個名爲Fieldtype.customtextfield.cshtml的新視圖。我需要知道的是:如何從自定義視圖訪問Width屬性?構建自定義字段類型:如何呈現自定義屬性?

這裏是我的代碼:

CustomTextfield.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using Umbraco.Forms.Core; 
using System.Web.UI.WebControls; 

namespace Custom.FieldType { 
    public class CustomTextfield : Umbraco.Forms.Core.FieldType { 
     public CustomTextfield() { 
      //Provider 
      this.Id = new Guid("b994bc8b-2c65-461d-bfba-43c4b3bd2915"); 
      this.Name = "Custom Textfield"; 
      this.Description = "Renders a html input fieldKey"; //FieldType 
      this.Icon = "textfield.png"; 
     } 

     public System.Web.UI.WebControls.TextBox tb; 
     public List<Object> _value; 

     [Umbraco.Forms.Core.Attributes.Setting("Width", description = "Enter the width of the Textfield")] 
     public string Width { get; set; } 

     public override WebControl Editor { 
      get { 
       tb.TextMode = System.Web.UI.WebControls.TextBoxMode.SingleLine; 
       tb.CssClass = "text"; 
       if (!string.IsNullOrEmpty(this.Width)) { 
        int width; 
        if (Int32.TryParse(this.Width, out width)) { 
         tb.Width = width; 
        } 
       } 
       if (_value.Count > 0) 
        tb.Text = _value[0].ToString(); 

       return tb; 
      } 
      set { base.Editor = value; } 
     } 

     public override List<Object> Values { 
      get { 
       if (tb.Text != "") { 
        _value.Clear(); 
        _value.Add(tb.Text); 
       } 
       return _value; 
      } 
      set { _value = value; } 
     } 

     public override string RenderPreview() { 
      return 
       "<input type=\"text\" id=\"text-content\" class=\"text\" maxlength=\"500\" style=\"" + this.Width + "px\" />"; 
     } 

     public override string RenderPreviewWithPrevalues(List<object> prevalues) { 
      return RenderPreview(); 
     } 

     public override bool SupportsRegex { 
      get { return true; } 
     } 

    } 
} 

Fieldtype.customtextfield.cshtml:

@model Umbraco.Forms.Mvc.Models.FieldViewModel 
<input type="text" name="@Model.Name" id="@Model.Id" class="text" value="@Model.Value" maxlength="500" 
style="@{if(!string.IsNullOrEmpty(Model.Width)){<text>width:@(Model.Width)px; </text>}}" 
@{if(Model.Mandatory || Model.Validate){<text>data-val="true"</text>}} 
@{if (Model.Mandatory) {<text> data-val-required="@Model.RequiredErrorMessage"</text>}} 
@{if (Model.Validate) {<text> data-val-regex="@Model.InvalidErrorMessage" data-regex="@Model.Regex"</text>}} 
/> 

的觀點不工作的代碼,因爲我想以引用不存在的Width屬性。我找不到使用剃刀的自定義屬性的自定義字段類型的任何示例。如果有人能指出我的方向正確,我會很感激。

回答

1

我找到了解決方案。原來FieldViewModel有一個AdditionalSettings屬性,是List<FieldSettingViewModel>型它由一個字符串KeyValue

的爲了讓我上面的例子中工作,我可以使用以下命令:

@model Umbraco.Forms.Mvc.Models.FieldViewModel 
@{ 
    var widthSetting = Model.AdditionalSettings.FirstOrDefault(s => s.Key.Equals("Width")); 
    string width = (widthSetting == null) ? null : widthSetting.Value; 
} 
<input type="text" name="@Model.Name" id="@Model.Id" class="text" value="@Model.Value" maxlength="500" 

    style="@{if(!string.IsNullOrEmpty(width)){<text>width:@(width)px; </text>}}" 

    @{if(Model.Mandatory || Model.Validate){<text>data-val="true"</text>}} 
    @{if (Model.Mandatory) {<text> data-val-required="@Model.RequiredErrorMessage"</text>}} 
    @{if (Model.Validate) {<text> data-val-regex="@Model.InvalidErrorMessage" data-regex="@Model.Regex"</text>}} 
/> 
相關問題