0

Anyone knows how to get list of selected CheckBoxList values from Umbraco using ContentService?

I use contentService.GetValue("currencies")

and I get string with numbers and commas something like "154,155,156,157"

How can I get actual values?

Does anyone know how to do it using DataTypeService?

3 Answers3

1

As it was mentioned above, you can use Umbraco helpers method to retrieve string value for the PreValue id.

Umbraco.GetPreValueAsString([PreValueId])

You can find more about it here: https://our.umbraco.org/documentation/reference/querying/umbracohelper/#getprevalueasstring-int-prevalueid

It's calling database directly through the DataTypeService behind the scene. It's worth to reconsider it and close it on another type of property / data type to avoid calling database on the frontend layer and just retrieve data from IPublishedContent cached model.

Read also about common pitfalls and anti-patterns in Umbraco: https://our.umbraco.org/documentation/Reference/Common-Pitfalls/

Marcin Zajkowski
  • 1,668
  • 10
  • 14
  • Hey, thanks for reply. One more question: I don't know Umbraco that good but normally in ASP.NET application you control cache and you break it when something changes and re-cache it. Does Umbraco take care about it? Does it break and re-cache if something changed in CMS/database? 2nd thing if I post my model that inherits from RenderModel my Content losing some data so it's not possible to retrieve it back so I need to get it from Umbraco somehow rather than posting everything. And last thing I get it back currently using ContentService - does ContentService takes it from DB or cache? – Krzysztof Tryc Apr 08 '17 at 21:16
  • ContentService is taking data straight from DB. Regarding IPublishedContent cache - yes, Umbraco is updating cache after each relevant action in the backoffice. There are also some lower profile options like Examine indexes, you can read more about them if you will need something really well optimized with a lot of data. Regarding models - check ModelsBuilder built-in future. It will give you strongly typed models which you can extend if you want to. – Marcin Zajkowski Apr 10 '17 at 08:00
0

Those are prevalues.

From that given string, you have to split those and get the real value by calling one of umbraco library umbraco.library.GetPreValueAsString(123);

For example.

foreach(var item in contentService.GetValue("currencies").Split(',')) {
  var realValue = umbraco.library.GetPreValueAsString(int.Parse(item);
}
  • thanks for your reply seems to be better than it was but I'm getting 2 null values. I checked and the number of currencies is ok so I'm getting all I need but why I'm getting these 2 nulls? Any idea why is that? And one more question before I was trying to do same using DataTypeService but it simply didn't work. Is it possible to do same using DataTypeService?https://i.stack.imgur.com/dZhpP.png – Krzysztof Tryc Apr 07 '17 at 16:46
  • I see you´re using contentservice in a controller method for the fronted of your website. It's better to use the UmbracoHelper classes for this. `UmbracoHelper.TypedContent(model.PaymentFormId)` – Mark Apr 07 '17 at 21:30
0

You have 2 nulls because prevalues sometimes has blank values on it, like "121,56,,15,145".

You need then to modify your split code like this

foreach (var item in value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{

}

StringSplitOptions.RemoveEmptyEntries will ignore empty spaces.

  • This doesn't explain why there are null values. Is it possible to do same using DataTypeService rather than umbraco.library ? – Krzysztof Tryc Apr 06 '17 at 19:42
  • I'm not sure why it has null values because I don't know yet how Umbraco saved those values, but checking it versus your data type values might help. Also using datatypeservice is calling directly to db which can slow down site. –  Apr 07 '17 at 02:49
  • ok thanks. So do you know how to do it with datatypeservice? Can you provide some sample? – Krzysztof Tryc Apr 07 '17 at 08:02