Copied to clipboard

Flag this post as spam?

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


  • Chris Lo 1 post 71 karma points
    Nov 22, 2016 @ 13:44
    Chris Lo
    0

    Get prevalues of dropdown using ContentService

    Hi all,

    Using the ContentService, I found that my dropdown was returning an int instead of the actual string value entered as the prevalue.

    I found this page:

    https://our.umbraco.org/forum/using-umbraco-and-getting-started/76225-getting-the-correct-value-from-a-dropdown

    ...which is getting me closer, so I now have this code:

            var contentType = Services.ContentTypeService.GetContentType("location");
        var contents = Services.ContentService.GetContentOfContentType(contentType.Id);
    
        try
        {
            var locations = contents.Where(x => x.Published
            && (x.GetValue<string>("City").ToLower() == model.City.ToLower() || string.IsNullOrWhiteSpace(model.City))
            && (Services.DataTypeService.GetPreValueAsString(Convert.ToInt32(x.GetValue<string>("State"))) == model.State || string.IsNullOrWhiteSpace(model.State)))
                .Select(t => new Location
                {
                    Name = t.Name,
                    StreetAddress = t.GetValue<string>("StreetAddress"),
                    State = umbraco.library.GetPreValueAsString(Convert.ToInt32(t.GetValue<string>("State"))),
                    Postcode = t.GetValue<int>("Postcode"),
                    City = t.GetValue<string>("City"),
                    Phone = t.GetValue<string>("Phone"),
                    Id = t.Id,
                    Geolocation = JsonConvert.DeserializeObject<Geolocation>(t.GetValue("geocoder").ToString())
                }).ToList();
    
            return locations;
        }
        catch (Exception ex)
        {
            Logger.Error(this.GetType(), ex.Message, ex);
            throw;
        }
    

    But I get this error:

    2016-11-23 00:46:11,628 [P11604/D48/T68] ERROR LocatorApiController - Object reference not set to an instance of an object. System.NullReferenceException: Object reference not set to an instance of an object. at LocatorApiController.<>cDisplayClass2_0.0(IContent x) in C:\Repositories...\Controllers\LocatorApiController.cs:line 91 at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at LocatorApiController.SearchLocation(LocatorFormViewModel model) in C:\Repositories...\Controllers\LocatorApiController.cs:line 91

    Not sure how to fix this?

    Thanks, Chris

Please Sign in or register to post replies

Write your reply to:

Draft