for some reason the media.HasValue("link") (or also if using HasProperty) always returns true.
I also tried to write out the value. if theres a value in the content picker it returns the ID of the referenced node (which is OK).
But.. if I delete the the reference in the content picker it returns 'umbraco.MacroEngines.DynamicXml'.??
If this is treated as positive result for the HasVAlue or HasProperty, its method is not of much use is it? or am I totally misunderstanding something here?
now the error is not thrown, but thre condtion is still 'true' even no content in contentPicker. I guess it now returns ID as string or umbraco.MacroEngines.DynamicXml
HasValue, HasProperty checking Content Picker with razor problem
Hi there
I really often run into problems when try to do conditional checks (if the property have value) on different umbraco properties, when using razors.
fx: a slideshow. - I added a generic property 'link' as a 'content picker' to the standard umbraco image doctype.
Now I want to check if the link is choosen by editor.
If so: let the image in slideshow link.
If not: then just ad the image.
tried different things fx
<ul>
@foreach (dynamic media in mediaItem.Children.Items)
{
<li>
@if(media.HasValue("link")){
dynamic anchorNode = new DynamicNode(media.link);
<a class="images" href="@anchorNode.Url">
<imgsrc="@media.umbracoFile"width="@media.umbracoWidth height="@media.umbracoHeight" alt="@media.Name" />
</a>
}else{
<img src="@media.umbracoFile" width="@media.umbracoWidth" height="@media.umbracoHeight" alt="@media.Name" />
}
</li>
}
</ul>
for some reason the media.HasValue("link") (or also if using HasProperty) always returns true.
I also tried to write out the value. if theres a value in the content picker it returns the ID of the referenced node (which is OK).
But.. if I delete the the reference in the content picker it returns 'umbraco.MacroEngines.DynamicXml'.??
If this is treated as positive result for the HasVAlue or HasProperty, its method is not of much use is it? or am I totally misunderstanding something here?
can anyone help me with this? :-)
best regards :-)
Nicolai
Hi Nicolai
What do you get if you change it to something like this
thx for taking time to answer
if use as suggested: @if(!String.IsNullOrEmpty(media.contentPicker)){
then I get the error
Error loading Razor Script Slideshow.cshtml
The best overloaded method match for 'string.IsNullOrEmpty(string)' has some invalid arguments
I then tried to convert it to a string (guess error is related to the fact that the contentPicker returns an int)
@if(!String.IsNullOrEmpty(media.contentPicker.ToString())){
now the error is not thrown, but thre condtion is still 'true' even no content in contentPicker.
I guess it now returns ID as string or umbraco.MacroEngines.DynamicXml
Which again is a positive statement
Try to use this:
I think this should work, I built function with that code that wiil always check to see if my property is empty.
Moran, i would say that one look right. Althought i would do:
DynamicNode ctxnode = new DynamicNode(@Model.context.Current.Id) (i think thats right)
Nichola, you always want to check wether the property exsist with:
node.HasProperty("")
Hope this helps :)
This actually solved it:
@if( @media.link.GetType().ToString() == "System.Int32" ){
not beautifull, but it works. Gues the main problem is that it returns an int if
content picker is choosen but a string if its empty.
So above converts it to a string , and then do the check on the string.
@Moran I will try and check you solution to tommorow morning
thx :-) Nicolai
@Charles my problem is that HasProperty returns true in both cases. - it returns umbraco.MacroEngines.DynamicXml If content picker is empty.
You're welcome :)
is working on a reply...