uComponents Multi-URL Picker - problems with outputted value
Hi Everyone,
I'm using a multi-url picker with Umbraco 4.7.1.1 and I'm trying to check whether or not the property is empty, and if it is empty, hide it's container div; should be fairly straight forward, but my problem is that when no nodes are selected, the property still has a value? I've tried various methods to no avail, but on some pages they work and some they don't. It seems that pages with this property which have had a node selected - then later removed - have a different value to pages that have never used this property.
Here's a few methods I've tried doing and as you can see, even trying them all together doesn't work; this code works somewhat, but the container div still shows on some pages:
@if((Model.multiUrlPicker.GetType() == typeof(DynamicXml)) || (Model.HasProperty("multiUrlPicker") && Model.GetProperty("multiUrlPicker").Value != "<multi-url-picker />") || (Model. multiUrlPicker.InnerText != "") || (Model.HasProperty("multiUrlPicker") && Model.GetProperty("multiUrlPicker").Value != "")) { @* Show container & list items *@ }else{ @* Don't show them *@ }
Are there any other methods to check whether or not this property is empty?
Personally I like to use a helper method to cast the value to MultiUrlPickerState - makes things nice and simple with a strongly typed object to work with. Here is the helper function I use (placed in App_Code):
public static MultiUrlPickerState GetMultiUrlPickerState(string value) { MultiUrlPickerState state = null; if (!string.IsNullOrEmpty(value)) { state = MultiUrlPickerState.Deserialize(value); } return state; }
Then to use it:
MultiUrlPickerState selectedLinks = GlobalHelpers.GetMultiUrlPickerState(Page.Model.GetProperty("selectedLinks").Value); if (selectedLinks.Items.Any()) { <div class="container"> @foreach (var link in selectedLinks.Items) { <a href="@link.Url">link</a> } </div> }
Note this will happen automatically in uComponents v4 via RazorDataTypeModels - but this is a good interim solution.
Yep, just add a .cs file to your App_Code folder, for example MyHelpers.cs. Then add the block above wrapped in @functions { }. Then you can access it from any Razor file by MyHelpers.GetMultiUrlPickerState
uComponents Multi-URL Picker - problems with outputted value
Hi Everyone,
I'm using a multi-url picker with Umbraco 4.7.1.1 and I'm trying to check whether or not the property is empty, and if it is empty, hide it's container div; should be fairly straight forward, but my problem is that when no nodes are selected, the property still has a value? I've tried various methods to no avail, but on some pages they work and some they don't. It seems that pages with this property which have had a node selected - then later removed - have a different value to pages that have never used this property.
Here's a few methods I've tried doing and as you can see, even trying them all together doesn't work; this code works somewhat, but the container div still shows on some pages:
Are there any other methods to check whether or not this property is empty?
Kind Regards,
mmmoustache
Hi,
Personally I like to use a helper method to cast the value to MultiUrlPickerState - makes things nice and simple with a strongly typed object to work with. Here is the helper function I use (placed in App_Code):
Then to use it:
Note this will happen automatically in uComponents v4 via RazorDataTypeModels - but this is a good interim solution.
HTH,
Tom
Hi Tom,
Thanks a lot for the info, just a quick question though: what file format does the helper function needs to be in? .cs?
Kind Regards,
mmmoustache
Yep, just add a .cs file to your App_Code folder, for example MyHelpers.cs. Then add the block above wrapped in @functions { }. Then you can access it from any Razor file by MyHelpers.GetMultiUrlPickerState
is working on a reply...