Copied to clipboard

Flag this post as spam?

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


  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jan 04, 2017 @ 14:24
    Simon Dingley
    0

    Setting Hidden Field Values to Current or parent page name

    I have a form that is reused in different places throughout a site and I need to populate a hidden field value with the name of a page if the current page is of the document type x and if the document type is actually of type y then I need to populate it with the parent page name. So essentially, I want to populate it with Model.Content.AncestorOrSelf("doctypealias").Name.

    Is this possible?

    Thanks, Simon

  • David Peck 690 posts 1896 karma points c-trib
    Jan 04, 2017 @ 15:01
    David Peck
    101

    Could you not create a custom field type and the put your code in to the razor view for for that field?

    It might need to be changed like so:

    @{
        var umbraco = new UmbracoHelper(UmbracoContext.Current);
        var  currentPage = umbraco.TypedContent(UmbracoContext.Current.PageId);
    }
    <input type="hidden" name="@Model.Name" id="@Model.Id" class="hidden" value="@(currentPage.AncestorOrSelf("doctypealias").Name)"/>
    
  • David Peck 690 posts 1896 karma points c-trib
    Jan 04, 2017 @ 15:01
  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jan 04, 2017 @ 18:23
    Simon Dingley
    0

    Thanks David, I had thought about it but wondered if there was an out of the box solution. If not then I will go down that route so thanks for the tip.

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jan 04, 2017 @ 19:14
    Simon Dingley
    2

    In case it's of help to anyone else I simply inherited from the existing HiddenField type and overrode the Id, FieldTypeViewName, Name & Description values as follows:

    public sealed class MyFieldType: HiddenField
    {
        public MyFieldType()
        {
            //Provider
            this.Id = new Guid("F7D1D505-4B28-4EDC-937F-500DF84F77AF");
            this.FieldTypeViewName = "FieldType.MyFieldType.cshtml";
            this.Name = "My Field Type";
            this.Description = "My field type description.";
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft