Copied to clipboard

Flag this post as spam?

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


  • ChristophC 22 posts 61 karma points
    Mar 28, 2019 @ 11:42
    ChristophC
    0

    Datatype prevalues in Umbraco 8

    Hey! I need to get the prevalues of a Data Type on my hompage. I´m using Umbraco 8. This is my master-head:

        @inherits Umbraco.Web.Mvc.UmbracoViewPage 
        @{
            Layout = null;
         }
    

    and this is my homepage-head:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.Homepage>
    @using ContentModels = Umbraco.Web.PublishedModels;
    
    @{
        Layout = "master.cshtml";
    }
    

    When I use

    @foreach (var categoryPrevalue in ApplicationContext.Services.DataTypeService.GetPreValuesByDataTypeId(1125).ToList())
    {
     <li>@categoryPrevalue</li>
    }
    

    I get the error:

    CS0103: The name 'ApplicationContext' does not exist in the current context

    What can I do?! Thanks and best regards, Christoph

  • Paul Seal 524 posts 2890 karma points MVP 7x c-trib
    Mar 29, 2019 @ 15:36
    Paul Seal
    0

    Try using

    Current.Services.
    

    to find the relevant service.

    Kind regards

    Paul

  • Kyle Jorve 11 posts 94 karma points
    Jan 07, 2020 @ 16:48
    Kyle Jorve
    0

    If I try this in a partial view it says "The name 'Current' does not exist in the current context."

  • Jonathon Cove 26 posts 101 karma points
    Feb 21, 2020 @ 09:56
    Jonathon Cove
    0

    I was able to get to the DataTypeService by using Services.DataTypeService in Umbraco 8.4.1

  • Karl Lee 4 posts 77 karma points
    Apr 01, 2019 @ 07:57
    Karl Lee
    2

    Hi, we created the following, to return the prevalues as a list:

    public static List<PreValue> GetDataTypePreValues(int id)
            {
                IDataTypeService dataTypeService = Current.Services.DataTypeService;
    
                List<PreValue> toReturn = new List<PreValue>();
    
                IDataType dataType = dataTypeService.GetDataType(id);
    
                if (dataType != null)
                {
                    ValueListConfiguration valueList = (ValueListConfiguration)dataType.Configuration;
    
                    if (valueList != null && valueList.Items != null && valueList.Items.Any())
                    {
                        toReturn.AddRange(valueList.Items.Select(s => new PreValue
                        {
                            Id = s.Id,
                            Value = s.Value
                        }));
                    }
                }
    
                return toReturn;
            }
    

    using the following class:

    public class PreValue
    {
            public int Id { get; set; }
    
            public string Value { get; set; }
    }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies