Copied to clipboard

Flag this post as spam?

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


  • David McGawn 37 posts 150 karma points
    Feb 04, 2016 @ 09:12
    David McGawn
    1

    Where am i going wrong

    Can anybody tell me where i am going wrong here? why wont it let me pass the string?

    <select class="form-control" id="CourseCodeSelect"> <option selected="selected" value="0">Choose a Course</option> @{ string Passfaculty = CurrentPage.facultyName; foreach (var pageCourse in @Model.Content.AncestorOrSelf(1).Children.Where(x => x.Name == "Courselist")){ foreach (var subpageCourse in Model.Children.Where("courseListFaculty = @0", "@Passfaculty")){ var content = Umbraco.TypedContent(@subpageCourse.Id); var schoolcode = content.GetPropertyValue("courseListSchoolCode"); <option value="@schoolcode">@subpageCourse.Name</option> } } } </select>

  • Daniel 60 posts 174 karma points
    Feb 04, 2016 @ 11:05
    Daniel
    0

    I see a couple of things that look a bit off:

    • Your select element's closing tag seems to be a script closing tag
    • Only use @ when you're outputting something to the markup, example: no need to use this in your foreach()
    • Your Model.Children.Where() seems a bit odd, perhaps you're looking to do something like:

      foreach (var subpageCourse in Model.Children.Where(x => x.GetPropertyValue("courseListFaculty").Equals("Passfaculty"))){
      

    Not sure if Passfaculty is a variable or not!

    • You're foraching pageCourse but not using the variable, perhaps you mean something like:

      foreach (var subpageCourse in pageCourse.Children.Where(x => x.GetPropertyValue("courseListFaculty").Equals("Passfaculty"))){
      

    A few things to try out :)

  • David McGawn 37 posts 150 karma points
    Feb 04, 2016 @ 11:27
    David McGawn
    0

    Your select element's closing tag seems to be a script closing tag - Yeah that was a mistake when writing it in here lol

    Only use @ when you're outputting something to the markup, example: no need to use this in your foreach() - Point noted thanks :-)

    none of the last two work, all i want it to do is take a value from the page that it is on and pass it to the varible and use it in the courseListFaculty = whatever that varible is so that the correct dropdown is displayed :-(

  • Daniel 60 posts 174 karma points
    Feb 04, 2016 @ 11:55
    Daniel
    0

    Oh I totally missed you putting Passfaculty in a variable, sorry!

    My guess of something that may work or perhaps give you a better error message:

    @{
        //You may wish to move this @block to the top of the page in time just to keep the markup more clear (eventually into Controller)
        //Changing a few things to make it strongly typed for better debugging
        var passfaculty = CurrentPage.GetPropertyValue<String>("facultyName"); //make sure the current page has a property with alias "facultyName"
    
        var home = CurrentPage.AncestorOrSelf(1);
        //You may wish to make this search based upon Document Type in time - also check the name of the page below home is exactly "Courselist"
        var courseListPage = home.Single(x => x.Name.Equals("Courselist"));
        //For the next line, make sure that the Children under the courseListPage have a property with the property alias of "courseListFaculty"
        var coursePages = courseListPage.Children.Select(x => x.GetPropertyValue<String>("courseListFaculty").Equals(passFaculty));
    }        
    
    <select class="form-control" id="CourseCodeSelect">
        <option selected="selected" value="0">Choose a Course</option>
        @foreach (var coursePage in coursePages){
            var schoolcode = coursePage.GetPropertyValue<String>("courseListSchoolCode"); //is this a string or int? I'm guessing String! - also make sure the course page has a property with alias "courseListSchoolCode"
            <option value="@schoolcode">@coursePage.Name</option>
        }
    </select>
    

    I may have made a few typos as all is done in this markdown editor, but give that a go and see if a better error message pops up! :)

  • David McGawn 37 posts 150 karma points
    Feb 04, 2016 @ 12:15
    David McGawn
    0
    @{
           var passfaculty = CurrentPage.GetPropertyValue<String>("facultyName"); 
               //make sure the current page has a property with alias "facultyName" -  It does
           var home = CurrentPage.AncestorOrSelf(1);
               //You may wish to make this search based upon Document Type 
                //in time - also check the name of the page below home is exactly "Courselist"
           var courseListPage = home.Single(x => x.Name.Equals("Courselist"));
               //For the next line, make sure that the Children under 
                //the courseListPage have a property with the property alias of "courseListFaculty" - it does
                var coursePages = courseListPage.Children.Select(x => x.GetPropertyValue<String>("courseListFaculty").Equals(passFaculty));
            }        
    
            <select class="form-control" id="CourseCodeSelect">
                <option selected="selected" value="0">Choose a Course</option>
                @foreach (var coursePage in coursePages){
                    var schoolcode = coursePage.GetPropertyValue<String>("courseListSchoolCode"); 
                    //is this a string or int? its a string :-)
                     //Im guessing String! - also make sure the course page has a property with 
                      //alias "courseListSchoolCode"
                    <option value="@schoolcode">@coursePage.Name</option>
                }
            </select>
    

    I have added a few comments but still getting an error

    Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

    below is where i am trying to pull them from its like a database table type of thing in the backend where the users can enter info into then by the faculty the correct list from the full list is displayed throughout the site :-)

    enter image description here

    enter image description here

  • Daniel 60 posts 174 karma points
    Feb 04, 2016 @ 12:23
    Daniel
    0

    It seems like I assumed wrong about "courseListFaculty" being a String in the line:

    var coursePages = courseListPage.Children.Select(x => x.GetPropertyValue<String>("courseListFaculty").Equals(passFaculty));
    

    Just a random guess here since I'm not 100% inside your solution, but maybe:

    var coursePages = courseListPage.Children.Select(x => x.Name.Equals(passFaculty));
    

    instead would do? Or perhaps something similar? This isn't very pretty, but perhaps it would work untill refactored?

  • David McGawn 37 posts 150 karma points
    Feb 04, 2016 @ 12:32
    David McGawn
    0

    Yeah that doesnt seem to work either :-( the facultyName is on the courselist repository and and also on the faculty pages of the site so what i am trying to do is say for instance the faculty "A Level" on the A Level page and that drop down would only show the A Level courses from the CourseList Repository it does work as shown below but each time a new faculy gets added i need to go add a new one for the marketing team :-(

    @if (faculty == "A Level"){
            foreach (var pageCourse in @Model.Content.AncestorOrSelf(1).Children.Where(x => x.Name == "Courselist")){
             foreach (var subpageCourse in pageCourse.Children.Where("courseListFaculty == \"A Level\"")){
              var content = Umbraco.TypedContent(@subpageCourse.Id);
              var schoolcode = content.GetPropertyValue("courseListSchoolCode");
              <option value="@schoolcode">@subpageCourse.Name</option>
             }
            }
           }
           else if (faculty == "Accountancy"){
            foreach (var pageCourse in @Model.Content.AncestorOrSelf(1).Children.Where(x => x.Name == "Courselist")){
             foreach (var subpageCourse in pageCourse.Children.Where("courseListFaculty == \"Accountancy\"")){
              var content = Umbraco.TypedContent(@subpageCourse.Id);
              var schoolcode = content.GetPropertyValue("courseListSchoolCode");
              <option value="@schoolcode">@subpageCourse.Name</option>
             }
            }
           }
          else if (faculty == "Marketing"){
           foreach (var pageCourse in @Model.Content.AncestorOrSelf(1).Children.Where(x => x.Name == "Courselist")){
            foreach (var subpageCourse in pageCourse.Children.Where("courseListFaculty == \"Marketing\"")){
             var content = Umbraco.TypedContent(@subpageCourse.Id);
             var schoolcode = content.GetPropertyValue("courseListSchoolCode");
             <option value="@schoolcode">@subpageCourse.Name</option>
            }
           }
          }
         else if (faculty == "Leadership and Management"){
           foreach (var pageCourse in @Model.Content.AncestorOrSelf(1).Children.Where(x => x.Name == "Courselist")){
            foreach (var subpageCourse in pageCourse.Children.Where("courseListFaculty == \"Leadership and Management\"")){
             var content = Umbraco.TypedContent(@subpageCourse.Id);
             var schoolcode = content.GetPropertyValue("courseListSchoolCode");
             <option value="@schoolcode">@subpageCourse.Name</option>
            }
           }
          }
        else if (faculty == "Procurement and Supply"){
           foreach (var pageCourse in @Model.Content.AncestorOrSelf(1).Children.Where(x => x.Name == "Courselist")){
            foreach (var subpageCourse in pageCourse.Children.Where("courseListFaculty == \"Procurement and Supply\"")){
             var content = Umbraco.TypedContent(@subpageCourse.Id);
             var schoolcode = content.GetPropertyValue("courseListSchoolCode");
             <option value="@schoolcode">@subpageCourse.Name</option>
            }
           }
          }    
          else if (faculty == "GCSE and IGCSE"){
           foreach (var pageCourse in @Model.Content.AncestorOrSelf(1).Children.Where(x => x.Name == "Courselist")){
            foreach (var subpageCourse in pageCourse.Children.Where("courseListFaculty == \"GCSE and IGCSE\"")){
             var content = Umbraco.TypedContent(@subpageCourse.Id);
             var schoolcode = content.GetPropertyValue("courseListSchoolCode");
             <option value="@schoolcode">@subpageCourse.Name</option>
            }
           }
          }
          else if (faculty == "Learning and Development"){
           foreach (var pageCourse in @Model.Content.AncestorOrSelf(1).Children.Where(x => x.Name == "Courselist")){
            foreach (var subpageCourse in pageCourse.Children.Where("courseListFaculty == \"Learning and Development\"")){
             var content = Umbraco.TypedContent(@subpageCourse.Id);
             var schoolcode = content.GetPropertyValue("courseListSchoolCode");
             <option value="@schoolcode">@subpageCourse.Name</option>
            }
           }
          }
          else if (faculty == "Human Resources"){
           foreach (var pageCourse in @Model.Content.AncestorOrSelf(1).Children.Where(x => x.Name == "Courselist")){
            foreach (var subpageCourse in pageCourse.Children.Where("courseListFaculty == \"Human Resources\"")){
             var content = Umbraco.TypedContent(@subpageCourse.Id);
             var schoolcode = content.GetPropertyValue("courseListSchoolCode");
             <option value="@schoolcode">@subpageCourse.Name</option>
            }
           }
          }
    

    Thanks

  • Daniel 60 posts 174 karma points
    Feb 04, 2016 @ 12:47
    Daniel
    0

    Aha, I'm not quite sure about these data types, but perhaps in your first post, you could just change the:

    foreach (var subpageCourse in Model.Children.Where("courseListFaculty = @0", "@Passfaculty")){
    

    to

    foreach (var subpageCourse in Model.Children.Where("courseListFaculty = @0", Passfaculty)){
    

    ?

  • David McGawn 37 posts 150 karma points
    Feb 04, 2016 @ 12:52
    David McGawn
    0

    Yeah thats what i thought at first but got this error

    "0" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.

    the thing that i am trying to achieve should be so easy it is bugging me how i cant solve it lol

  • David McGawn 37 posts 150 karma points
    Feb 04, 2016 @ 13:13
    David McGawn
    0

    enter image description here

    I now have a new error :-(

  • Daniel 60 posts 174 karma points
    Feb 04, 2016 @ 13:20
    Daniel
    0

    What if you instead do:

    pageCourse.AsDynamic().Children
    

    in the line of the error?

  • David McGawn 37 posts 150 karma points
    Feb 04, 2016 @ 13:22
    David McGawn
    0

    Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Umbraco.Web.Models.PublishedContentWithKeyBase' does not contain a definition for 'GetPropertyValue'

    that is the error i get is i add AsDynamic so looks like we are getting closer :-)

  • Daniel 60 posts 174 karma points
    Feb 04, 2016 @ 13:23
    Daniel
    100

    then

    //var content = Umbraco.TypedContent(...);
    var schoolcode = subpageCourse.courseListSchoolCode;
    

    maybe?

    Dynamic types, what a mess... :)

  • David McGawn 37 posts 150 karma points
    Feb 04, 2016 @ 13:26
    David McGawn
    0

    Yeah thanks, i just had a total code mind block today that is it working perfect :-)

  • Daniel 60 posts 174 karma points
    Feb 04, 2016 @ 13:27
    Daniel
    1

    No worries! Just be sure to refactor it at some point, future you will thank you endlessly! :)

Please Sign in or register to post replies

Write your reply to:

Draft