Can not I create CONDITIONAL IF ELSE using var selection = Model.Content.Site()? Why?
Hi there,
I am trying do this on the Umbraco V7:
I have pagination that works very well and I have sort alphabetical that in the same way works very well, however, I need now other thing, I need check if someone search by name through my form this work nice, but my question is:
I can not create CONDITIONAL IF ELSE using var selection = Model.Content.Site()... this show me error all the time, but if I put outside the CONDITIONAL works great.
Please, find below an example which I am trying to do.
What error are you getting? From reading your code, it looks like one issue might be that you are missing a closing bracket ) in both of your Where calls.
I'm really sorry as I don't want to sound patronising but are you sure its not related to the missing bracket? I just want to make sure you've added it as the two code samples you've given don't contain it. The error you've just given would suggest it might be related to it.
@{
string searchterm = !string.IsNullOrEmpty(Request.Form["searchterm"]) ? Request.Form["searchterm"] : "";
var error = false;
var number = 0;
}
@if(!string.IsNullOrEmpty(searchterm)){
var selection = Model.Content.Site().FirstChild("main").FirstChild("news").Children()
.Where(x => x.IsVisible() && (x.GetPropertyValue<string>("dealerName").Contains(searchterm.ToUpper())));
number = selection.Count();
if(number > 0){
continue my code...
}
}
The problem was: When I was trying to get the selection value outside the conditional, its caused the error. I created everything inside the same IF and now everything works perfectly.
I guess if you create this var selection = Model.Content.Site()... inside the any conditional and try call it outside the conditional this action will create errors.
Can not I create CONDITIONAL IF ELSE using var selection = Model.Content.Site()? Why?
Hi there,
I am trying do this on the Umbraco V7:
I have pagination that works very well and I have sort alphabetical that in the same way works very well, however, I need now other thing, I need check if someone search by name through my form this work nice, but my question is:
I can not create CONDITIONAL IF ELSE using var selection = Model.Content.Site()... this show me error all the time, but if I put outside the CONDITIONAL works great.
Please, find below an example which I am trying to do.
By the way thank you so much for your help and contribution.
Hi Leonardo,
What error are you getting? From reading your code, it looks like one issue might be that you are missing a closing bracket
)
in both of yourWhere
calls.Thanks, Dan.
Dear Dan,
It is not related to the bracket ). It is funny because outside IF works.
Who it is possible?
About the error? So. Only show to me
Error loading Partial View script
Hey Dan,
This is the error:
Hi Leonardo,
I'm really sorry as I don't want to sound patronising but are you sure its not related to the missing bracket? I just want to make sure you've added it as the two code samples you've given don't contain it. The error you've just given would suggest it might be related to it.
Thanks, Dan.
Hi Dan,
Relax. :)
I understand you.
But I have checked, there is not any brackets missing.
Anyway, please, check it out:
Ah my bad. That makes sense. You're not missing a bracket at all :)
Dan, it is ok!
Check:
I have found it:
https://our.umbraco.org/forum/developers/razor/45189-Error-when-running-lambda-query-in-Razor
And now its become be more weird:
If I try it:
Its work. I can not understand.
Hi Leonardo
The only thing that is worried me that .OrderBy(x => x.Name) should be strongly typed, like that:
The code is working for me, if it's not for you, then the issue is with data, so something wrong with data coming from Umbraco
/Alex
Hi there,
Thank you so much for your help and support.
I have discovered the error.
I have done it:
The problem was: When I was trying to get the selection value outside the conditional, its caused the error. I created everything inside the same IF and now everything works perfectly.
I guess if you create this var selection = Model.Content.Site()... inside the any conditional and try call it outside the conditional this action will create errors.
Hi,
This is because it's not in scope outside of where it's declared.
If you want to declare var selection in the top section to use in other conditionals you can do this (note untested, not at my own machine).
Hi Steve, You rock.
Thank you so much!
is working on a reply...