Im trying to make a check on a field - I only want the HTML to output if the fields isn't empty - However, cant quite get it to work - What im doing is this?
Hi, actually when you write "ToString()" everything is casted to string. Otherwise despite the compiletime-type of the parameter you pass to the isNullOrEmpty() is dynamic the method "IsNullOrEmty" expects that the runtime-type of the parameter would be exactly the "string" type. So that if it's not a case then a runtime error is thrown.
Just for info: in .Net 4 you could replace the call to IsNullOrEmpty by a call to IsNullOrWhiteSpace. This does the same checks but also returns true if the string parameter consists of white spaces.
Simple check on field wont work
Im trying to make a check on a field - I only want the HTML to output if the fields isn't empty - However, cant quite get it to work - What im doing is this?
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
if(string.IsNullOrEmpty(@Model.whiteBoxText.ToString())){
<article class="other-jobs grey-gradient" >
<div class="text">
@Model.whiteBoxText
</div>
<div style="position:absolute; bottom:0; right:0;"><img src="@Model.Media("whiteBoxImage", "umbracoFile")" /></div>
</article>
}
}
Im i remove the ToString() i get an error saying:
Error loading Razor Script RenderBoxJob.cshtml
The best overloaded method match for 'string.IsNullOrEmpty(string)' has some invalid arguments
Try updating the if to the following:
Then i get this error:
Error loading Razor Script RenderBoxJob.cshtml
Cannot invoke a non-delegate type
By the way, it is a Rich Text Editor field i want to make the check on, if that makes any difference?
Isolate a test and rip everything else out except @Model.WhiteBoxText
If i remove everything, the field is being outputted...
Doh, just realised it might be this:
I think you have to change the quotes to:
<div style="position:absolute; bottom:0; right:0;"><img src='@Model.Media("whiteBoxImage", "umbracoFile")' /></div>
Then it works :) :) :)
But any idea why it fails with HasValue() ?
It works with the solution:
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
if(@Model.whiteBoxText != null && @Model.whiteBoxText.ToString() != ""){
<article class="other-jobs grey-gradient" >
<div class="text">
@Model.whiteBoxText
</div>
<div style="position:absolute; bottom:0; right:0;"><img src="@Model.Media("whiteBoxImage", "umbracoFile")" /></div>
</article>
}
}
Yea it's probably because it's case sensitive. You should be checking for "whiteBoxImage" instead of "WhiteBoxImage" I think?
EDIT: oh my first solution worked? lol I deleted it thinking it was wrong, ha.
Hi Nicky,
I think your test should be:
if(!string.IsNullOrEmpty(@Model.whiteBoxText.ToString())){
instead of
if(string.IsNullOrEmpty(@Model.whiteBoxText.ToString())){
If you want to write ou the HTMl on not empty nodes.
Cheers,
Michael.
^ Ahhh, that's exactly it.. my silly eyes are failing me today!
Hi, actually when you write "ToString()" everything is casted to string. Otherwise despite the compiletime-type of the parameter you pass to the isNullOrEmpty() is dynamic the method "IsNullOrEmty" expects that the runtime-type of the parameter would be exactly the "string" type. So that if it's not a case then a runtime error is thrown.
Just for info: in .Net 4 you could replace the call to IsNullOrEmpty by a call to IsNullOrWhiteSpace. This does the same checks but also returns true if the string parameter consists of white spaces.
Cheers,
Michael.
is working on a reply...