Copied to clipboard

Flag this post as spam?

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


  • James Feeney 26 posts 166 karma points
    Sep 11, 2013 @ 12:57
    James Feeney
    0

    AncestorOrSelf and Request.QueryString

    Hi,

    I'm collecting all items that have a parent equal to a querystring in my URL. However, my code is coming up with errors. Can anyone check that I'm doing this right? Thanks.

    The url contains at the end: ?Bottle=Nameofbottle (this works with other calls elsewhere)

    foreach (var recipe in Model.AncestorOrSelf(Request.QueryString["Bottle"]));
    { do stuff with recipes }

    The error: Object reference not set to an instance of an object.

    I do sometimes have spaces in my querystring. So a string might look like this: All%20Products
    Would this effect it in anyway?

  • James Feeney 26 posts 166 karma points
    Sep 11, 2013 @ 13:19
    James Feeney
    0

    Ok, I got the querysting to work by putting it in a string first. But it's not working in my foreach and I'm not sure why:

    @* get string from url *@
    @{
    string collect = String.Format("{0}", Request.QueryString["Bottle"]);
    }
    @collect  @* THIS WORKS *@

    @if (!string.IsNullOrEmpty(Request.QueryString["Bottle"]))  {
    foreach (var recipe in Model.RecipeRepositories.AncestorOrSelf(@collect))  {
    @* THIS DOES NOT WORK *@
    }
    }  

     

    Are there better ways of doing this? please do tell :) Thanks 

     

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 11, 2013 @ 20:41
    Jeavon Leopold
    0

    Hi James,

    I think the problem is that RecipeRepositories is being requested as a property (and a property doesn't have Ancestors), maybe it's a document type or a node name?

    Also what value are you passing in the "Bottle" querystring variable, document type alias?

    Thanks,

    Jeavon

  • James Feeney 26 posts 166 karma points
    Sep 12, 2013 @ 14:36
    James Feeney
    100

    Found the solution. You can see the correct code for the 'foreach' on another of my posts here: http://our.umbraco.org/forum/developers/razor/44701-Collect-items-from-variable-repository-name

Please Sign in or register to post replies

Write your reply to:

Draft