Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bob Davidson 7 posts 37 karma points
    Feb 28, 2013 @ 17:18
    Bob Davidson
    0

    Accessing Settings Attribute values from MVC views

    Hi,

    Is it possible to access Umbraco.Forms.Core.Attributes.Setting values from the MVC view of a custom field type?  For example, let's say you have a custom textfield FieldType, and you want to give the editor the ability to set the maxlength attribute per field in a form.  You might have something like:

        public class CustomInput : Umbraco.Forms.Core.FieldType {
            [Umbraco.Forms.Core.Attributes.Setting("Maxlength", control = "Umbraco.Forms.Core.FieldSetting.TextField")]
            public string MaxLength { get; set; }

            public CustomInput() {
                this.Id = new Guid("0631C5F5-B8C1-4DDE-8166-04C6B4EC30F3");
                this.Name = "Custom Text Field";
                this.Description = "Custom Text Field";

                this.Icon = "textfield.png";
                this.DataType = Umbraco.Forms.Core.FieldDataType.String;
            }

            public override string RenderPreview() {
                return "<input type=text size=6 maxlength=" + MaxLength + " />";
            }
        }

    How in MVC would I access the value an editor put in the MaxLength attribute?

    @model Umbraco.Forms.Mvc.Models.FieldViewModel

    <input type="text" name="@Model.Name" id="@Model.Id" class="text" value="@Model.Value"
    @{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>}}
    @{if (!string.IsNullOrEmpty(Model.????)) {<text> maxlength="@Model.????"</text>}}
    />

    Is this possible?  I couldn't find any examples of such access in the documentation.

    Thanks!

  • Comment author was deleted

    Feb 28, 2013 @ 17:28

    Yeah 

    Model.AdditionalSettings.FirstOrDefault(x => x.Key == "MaxLength").Value

  • Bob Davidson 7 posts 37 karma points
    Feb 28, 2013 @ 17:31
    Bob Davidson
    0

    Tim,

    Thanks for the quick response!  But FieldViewModel doesn't seem to have AdditionalSettings defined.  I'm getting a compiler error:

     'Umbraco.Forms.Mvc.Models.FieldViewModel' does not contain a definition for 'AdditionalSettings' 

    I'm using Contour 3.0.2.  Is this maybe a newer feature than the version I'm running?

    Thanks

  • Comment author was deleted

    Feb 28, 2013 @ 17:34
  • Bob Davidson 7 posts 37 karma points
    Mar 01, 2013 @ 00:04
    Bob Davidson
    0

    Upgrading to 3.0.6 fixed it.  Thanks for the help!

Please Sign in or register to post replies

Write your reply to:

Draft