Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Leonardo Moura 21 posts 90 karma points
    Feb 15, 2018 @ 14:36
    Leonardo Moura
    0

    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.

    string letter = "";
    string search = "";
    char l;
    
    
    if(!string.IsNullOrEmpty(Request.QueryString["search"])){
    
            search  =   Request.QueryString["search"];
            var selection = Model.Content.Site().FirstChild("main").FirstChild("news").Children()
                    .Where(x => x.IsVisible() &&  x.GetPropertyValue<string>("newsName")
                    .ContainsAny(!string.IsNullOrEmpty(search) ? search : "Not found"))
                    .OrderBy("Name");
    
    }else if(!string.IsNullOrEmpty(Request.QueryString["letter"])){
            letter = Request.QueryString["letter"];
    
            var selection = Model.Content.Site().FirstChild("main").FirstChild("news").Children()
                    .Where(x => x.IsVisible() &&  x.GetPropertyValue<string>("newsName").Substring(0,1)
                    .ContainsAny(!string.IsNullOrEmpty(letter) ? letter : "#,1,2,3,4,5,6,7,8,9,0"))
                    .OrderBy("Name");           
    
    }
    

    By the way thank you so much for your help and contribution.

  • Dan Lister 416 posts 1974 karma points c-trib
    Feb 15, 2018 @ 14:57
    Dan Lister
    0

    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 your Where calls.

    Thanks, Dan.

  • Leonardo Moura 21 posts 90 karma points
    Feb 15, 2018 @ 15:13
    Leonardo Moura
    0

    Dear Dan,

    It is not related to the bracket ). It is funny because outside IF works.

    if(!string.IsNullOrEmpty(Request.QueryString["letter"])){
            letter = Request.QueryString["letter"];
    
    }
    
    var selection = Model.Content.Site().FirstChild("main").FirstChild("news").Children()
                    .Where(x => x.IsVisible() &&  x.GetPropertyValue<string>("newsName").Substring(0,1)
                    .ContainsAny(!string.IsNullOrEmpty(letter) ? letter : "#,1,2,3,4,5,6,7,8,9,0"))
                    .OrderBy("Name");
    

    Who it is possible?

    About the error? So. Only show to me

    Error loading Partial View script

  • Leonardo Moura 21 posts 90 karma points
    Feb 15, 2018 @ 16:42
    Leonardo Moura
    0

    Hey Dan,

    This is the error:

    CS1593: Delegate 'System.Func

  • Dan Lister 416 posts 1974 karma points c-trib
    Feb 15, 2018 @ 16:45
    Dan Lister
    0

    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.

  • Leonardo Moura 21 posts 90 karma points
    Feb 15, 2018 @ 16:53
    Leonardo Moura
    0

    Hi Dan,

    Relax. :)

    I understand you.

    But I have checked, there is not any brackets missing.

    Anyway, please, check it out:

    enter image description here

  • Dan Lister 416 posts 1974 karma points c-trib
    Feb 15, 2018 @ 16:55
    Dan Lister
    0

    Ah my bad. That makes sense. You're not missing a bracket at all :)

  • Leonardo Moura 21 posts 90 karma points
    Feb 15, 2018 @ 16:59
    Leonardo Moura
    0

    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:

    if(!string.IsNullOrEmpty(Request.QueryString["search"])){    
    
    search  =   Request.QueryString["search"];
    var selection = Model.Content.Site().FirstChild("main").FirstChild("news").Children().Where(x => x.IsVisible()).OrderBy("Name");
    
    }
    

    Its work. I can not understand.

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Feb 16, 2018 @ 08:25
    Alex Skrypnyk
    1

    Hi Leonardo

    The only thing that is worried me that .OrderBy(x => x.Name) should be strongly typed, like that:

        var selection = Model.Content.Site().FirstChild("main").FirstChild("news").Children()
                .Where(x => x.IsVisible() && x.GetPropertyValue<string>("newsName")
                .Substring(0, 1).ContainsAny(!string.IsNullOrEmpty(letter) ? letter : "#,1,2,3,4,5,6,7,8,9,0"))
                .OrderBy(x => x.Name);
    

    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

  • Leonardo Moura 21 posts 90 karma points
    Feb 16, 2018 @ 14:40
    Leonardo Moura
    0

    Hi there,

    Thank you so much for your help and support.

    I have discovered the error.

    I have done 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.

  • Steve Morgan 1345 posts 4452 karma points c-trib
    Feb 16, 2018 @ 14:54
    Steve Morgan
    2

    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).

    @{ 
       string searchterm = !string.IsNullOrEmpty(Request.Form["searchterm"]) ? Request.Form["searchterm"] : "";
       var error  = false;
       var number = 0;
       var selection = Enumerable.Empty<IPublishedContent>();
    }
    @if(!string.IsNullOrEmpty(searchterm)){
             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...
            }
    }
    
  • Leonardo Moura 21 posts 90 karma points
    Feb 16, 2018 @ 15:12
    Leonardo Moura
    0

    Hi Steve, You rock.

    Thank you so much!

Please Sign in or register to post replies

Write your reply to:

Draft