Copied to clipboard

Flag this post as spam?

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


  • Scott Ritshie 35 posts 100 karma points
    Nov 14, 2014 @ 03:29
    Scott Ritshie
    0

    How to create a basic item detail page

    Hello,

    While I've created many Umbraco macros, I've never created an item details page that filters from a querystring "id" passed.

    First, I've created a macro that lists people with friendly URLs like www.mysite.com/people/ga/atlanta/joe-blow.aspx.  The link looks like:

    <a href="@umbracoHelper.NiceUrl(item.id)">View Bio</a>

    It doesn't include the item id in the URL, but I'm using a URL rewrite rule in IIS that I think might extract it, but may be wrong:

        <rewrite>
          <rules>
            <rule name="SpecificRewrite" stopProcessing="true">
              <match url="^people/(.+)" ignoreCase="false" />
              <action type="Rewrite" url="/personProfile.aspx?id={R:1}" />
            </rule>
          </rules>
        </rewrite>
    

    Then, I have a .cshtml macro in personProfile.master that I'm not sure how to get the details of that particular person node:

    @using System.Web.Mvc;
    @using Umbraco.Core.Models;
    @using Umbraco.Core.Services;
    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{
      var root = Model.AncestorOrSelf(1);
      var person_profile = root.Children.Where("NodeTypeAlias == \"PersonProfile\"").Where("id == " + Request.Url["id"]);
    }
    
    

    @person_profile.NodeName

    This creates an error.  Maybe I don't want a macro, but a partial page?  There's no foreach... just extracting child nodes and values from the model by id.

    Please help!

    Many thanks,
    Scott

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Nov 14, 2014 @ 12:14
    Steve Morgan
    0

    Hi Scott,

    Do you just want to read the URL parameter from the query string? As in, end users will hit  www.mysite.com/people/ga/atlanta/joe-blow.aspx?id=123

          // page param

    int profileId = 1;

        if (!int.TryParse(Request.Params["id"], out pageId))

        {

            profileId= 1;

        }

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Nov 14, 2014 @ 12:16
    Steve Morgan
    0

    Then you just need to amend your code to tack on the profileID - the default of 1 is just an example - you'd probably want to do some more complicated logic here (listing profiles or what have you). 

  • Charles Afford 1163 posts 1709 karma points
    Nov 14, 2014 @ 13:22
    Charles Afford
    0

    Scott how are you using this 'marco' Do you mean a view / partial view or a macro you will use in the RTE ect?

    Charlie :)

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Nov 14, 2014 @ 13:36
    Steve Morgan
    0

    Good point Charles - I read this as a Partial View (Razor) that just needs to display the relevant profile on this page.... I always read Macro as "Partial View", probably a bit dangerous to assume!

  • Scott Ritshie 35 posts 100 karma points
    Nov 14, 2014 @ 17:30
    Scott Ritshie
    0

    The URL should pass the ID without a querystring.  NiceURL doesn't seem to include the ID, so...

          www.mysite.com/people/ga/atlanta/joe-blow.aspx.

    Should really be

          www.mysite.com/people/ga/atlanta/joe-blow/12.aspx.

    Not sure how to make that change.

    And yes, Charles and Steve... I'm thinking this should probably be a partial view, rather than macro.  Macros typically use loops, right?  No need to loop values on a detail page... just grab values of child nodes.

    Which brings me to issue #2.  I've tried uQuery and was able to get the parent node name, which is the person's full name.  However, for the life of me I can't figure out how to grab child node values.

    Below, <p>@bio</p> doesn't throw an error (I'm currently using a macro, but I can change that to partial view):, but  <p>@bio.Name</p> does.

    @using umbraco;
    @using umbraco.NodeFactory;
    @using Umbraco.Core.Models;
    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{
      var id = Request.QueryString["id"];
      Node node = uQuery.GetNode(id);
      Node bio = node.GetChildNodeByName("bio");
    
      if (node != null)
      {
        <p>@node.Name</p>
        <p>@bio</p>
      }
      else
      {
        Response.Redirect("/people.aspx");
      }
    }

    So I have 2 issues:

    1. Passing the ID to the URL without a QS
    2. Getting child node values to show.  Willa  partial view render razor differently than a macro?
    Thanks for the help so far... I pulled my hair out for hours last night getting nowhere.  lol
  • Charles Afford 1163 posts 1709 karma points
    Nov 14, 2014 @ 18:05
    Charles Afford
    0

    Ha i do the same thing Steve.

    Scott i think you / we are a bit lost. What are you actually trying to achieve with this page?

    Why do you want the node id on the end of the Url? As Steve says this should be a query string and not stuck on the end of the url

    Take a look at http://our.umbraco.org/documentation/reference/templating/macros/ for macros

    Take a look at http://www.w3schools.com/aspnet/mvc_intro.asp for MVC

    A few problems :)

    First is this line:

    Request.QueryString["id"];

    This is looking for the query string on the URL

    so it should be:

    www.mysite.com/people/ga/atlanta/joe-blow/?id = 12

    This url has a valid query string and your Request.QueryString["id"] will return 12.

    What version of Umbraco are you using? umbraco.MacroEngines.DynamicNodeContext that seems odd to me.

    You should not get the nodes by name GetChildNodeByName() this can change and effectively destroys the use of the CMS, you are better of looking up the nodes by docTypeAlais

    I think you need to tell us

    What you are trying to achieve What pages you have Are you using MVC or web forms? What version of Umbraco

    Reply back and we will help you out :). Charlie

  • Scott Ritshie 35 posts 100 karma points
    Nov 14, 2014 @ 18:32
    Scott Ritshie
    0

    Charlie,

    Sorry about the confusion!

    Basically, I want to build an item detail page.  Here's a sample of parent/child nodes for each person from umbraco.config:

    <PersonProfile id="1085" parentID="1079" level="5" creatorID="0" sortOrder="2" createDate="2014-11-13T14:22:53" updateDate="2014-11-14T03:31:18" nodeName="Joe Blow" urlName="joe-blow" path="-1,1059,1068,1078,1079,1085" isDoc="" nodeType="1065" creatorName="Scott Ritshie" writerName="Scott Ritshie" writerID="0" template="1086" nodeTypeAlias="PersonProfile">
    <firstName>Joe</firstName>
    <lastName>Blow</lastName>
    <longBio><![CDATA[<p>Blah blah.</p>]]></longBio>
    <specialties><![CDATA[<p>Blah blah</p>]]></specialties>
    <email>[email protected]</email>
    <phone>123-456-7890</phone>
    <readingChargeAndDuration><![CDATA[<table border="0" cellpadding="5">
    <thead>
    <tr>
    <td>Duration</td>
    <td>Description</td>
    <td>Price</td>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td>1 hour</td>
    <td>In-depth Reading</td>
    <td>$125</td>
    </tr>
    <tr>
    <td>30 minutes</td>
    <td>In-depth Reading</td>
    <td>$65</td>
    </tr>
    <tr>
    <td>1 hour</td>
    <td>Extended Reading</td>
    <td>$150</td>
    </tr>
    <tr>
    <td>30 minutes</td>
    <td>Extended Reading</td>
    <td>$75</td>
    </tr>
    </tbody>
    </table>]]></readingChargeAndDuration>
    <shortBio><![CDATA[Blah blah blah.]]></shortBio>
    <photo>1072</photo>
    </PersonProfile>

    Problem 1:  I can't figure out how to get child node values.

    Problem 2:  Querystrings are bad for SEO, so I'd prefer to use routing values (such as in web.config) to pull the id from the friendly URL, such as www.mysite.com/people/ga/atlanta/joe-blow/12.aspx.  NiceUrl doesn't pass the ID, so I guess I'll need to setup a custom routing engine.

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Nov 14, 2014 @ 18:42
    Steve Morgan
    0

    Before you get too far into custom routing. Are you sure that you can't achieve what you need by just structuring you Umbraco nodes? How does your data structure look?

    Homepage
    - People
    - State
    - City
    - Dave Somebody
    - Joe Blow 
    - .... 

    If it's like the above - then your nodes can serve as pages - no need for clever URL rewriting. Let Umbraco's tree structure dictate the URL and use Partial View macros to list children. For example in the city template create a macro that lists all nodes of doc type "person" below. I'm struggling to see why you need the ID - perhaps we're missing something but you seem to be looking at this with a bit too much of an MVC eye. 

    As an SEO note (at the risk of getting shot down but SEO specialists..) these days URL variables are not the evil they once were, Google understands them - though I don't think you need them if all you're doing is listing children and linking to them!?!? 

  • Scott Ritshie 35 posts 100 karma points
    Nov 14, 2014 @ 19:59
    Scott Ritshie
    0

    I just figured the first part out.  I can get the parent "PersonProfile" node like this:

    @using umbraco;
    @using umbraco.NodeFactory;
    @*@using umbraco.MacroEngines;*@
    @*@using Umbraco.Core.Models;*@
    @*@inherits umbraco.MacroEngines.DynamicNodeContext*@

    @{
    string url = Context.Request.RawUrl;
    Node personNode= uQuery.GetNodeByUrl(url);
    int nodeId = -1;

    try
    {
    nodeId = personNode.Id;
    }
    catch { }

    if (nodeId == -1)
    {
    Response.Redirect("/people.aspx");
    }

    <p>@personNode.Name</p>
    }

    Works like a champ without having to extract the id from the QS.  Yep... I was thinking too much about MVC.  ;o)

    So now the issue is how to get the child node values from @personNode.  Please see the Umbraco node tree above.  For example, <longBio /> has a value.  How do I parse that from the root node @personNode?

  • Charles Afford 1163 posts 1709 karma points
    Nov 14, 2014 @ 20:04
    Charles Afford
    0

    I agree Steve :). SCOTT SLOW DOWN!

    Are you using mvc? The code you have above is it in a view or a partial view?

    What version of Umbraco are you using?

    Do not write or edit more code, you will go around in circles :).

    Also you should not have a try catch block there, that is just wrong :).

    Why are you trying to get a node by Path?

    Can you show us the content structure laid out like Steve has done above please :).

    Also what are the doc types or your nodes?

    Charlie :)

  • Charles Afford 1163 posts 1709 karma points
    Nov 14, 2014 @ 20:10
    Charles Afford
    0

    Also if its a view you should be using @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    Then you can use @Model.GetPropertyValue() and Model.Children things like that :) take a look at:

    http://our.umbraco.org/documentation/Reference/Templating/Mvc/

  • Scott Ritshie 35 posts 100 karma points
    Nov 14, 2014 @ 20:13
    Scott Ritshie
    0

    I explained this in the beginning of my post.  I know you can get lost reading a lot of code, so sorry for the confusion.  :P

    I have a .cshtml macro (revised code in my last post) set to view in personProfile.master.  I can now get the correct name from the PersonProfile node, but my remaining problem is displaying child node values.

    I'm using 6.2.4 within VS 2013. No custom classes, controllers, or MVC views... just docTypes and macros created straight through Umbraco and edited in VS.

    Is using the macro as I am ok to do?

  • Scott Ritshie 35 posts 100 karma points
    Nov 14, 2014 @ 20:13
    Scott Ritshie
    0

    I explained this in the beginning of my post.  I know you can get lost reading a lot of code, so sorry for the confusion.  :P

    I have a .cshtml macro (revised code in my last post) set to view in personProfile.master.  I can now get the correct name from the PersonProfile node, but my remaining problem is displaying child node values.

    I'm using 6.2.4 within VS 2013. No custom classes, controllers, or MVC views... just docTypes and macros created straight through Umbraco and edited in VS.

    Is using the macro as I am ok to do?

  • Charles Afford 1163 posts 1709 karma points
    Nov 14, 2014 @ 20:16
    Charles Afford
    0

    Sorry we have confusion here. Are you using MVC, because personProfile.Master is webforms?

    The reason for asking for the content structure is the macro does not sound like the write thing to be using?

    Charlie.

  • Scott Ritshie 35 posts 100 karma points
    Nov 14, 2014 @ 20:25
    Scott Ritshie
    0

    Charlie,

    Here's the content structure in Umbraco:

    Homepage
    - About
    -People
    ---- GA
    --------Atlanta
    ------------Joe Blow
    - Contact

    What's wrong with try/catch?  It's a great way to do a redirect, based on null content (such as someone editing the URL).  I'm extracting the path to get the node via the id.

    This is more like thinking from a .NET forms approach, rather than MVC.  I'm an MVC newbie and my wires are crossed how to get the model using a partial view template rendered in a .master page template.

  • Charles Afford 1163 posts 1709 karma points
    Nov 14, 2014 @ 20:39
    Charles Afford
    0

    Ok so what data do you want to display on the page from those nodes and what are the properties on the nodes?

    I am assuming that you have a template on People and when you go to the people page you want that page to display all of the people underneath it?

    What is the document types on the Joe Blow node?

    Well the problem with the try catch is you are not really trying to do anything and not going to catch anything.

    Yours:

    @{ int nodeId = -1; try { nodeId = personNode.Id; } catch { }

    if (nodeId == -1) { Response.Redirect("/people.aspx"); } }

    Mine:

    @{

    int nodeId = 0; if(personNode!= null && personNode.Id > 0) { Response.Redirect("/people.aspx") } }

  • Charles Afford 1163 posts 1709 karma points
    Nov 14, 2014 @ 20:40
    Charles Afford
    0

    On what page is this getting called?

  • Scott Ritshie 35 posts 100 karma points
    Nov 14, 2014 @ 20:43
    Scott Ritshie
    0

    This is really just a simple profile page where you can display the details (child nodes) of a person.  The People page displays a list of people, each of which click to a detail page.

  • Charles Afford 1163 posts 1709 karma points
    Nov 14, 2014 @ 20:45
    Charles Afford
    0

    Scott could you answer my questions and i will help :).

    Charlie

  • Scott Ritshie 35 posts 100 karma points
    Nov 14, 2014 @ 20:47
    Scott Ritshie
    0

    The People page lists people, each of which clicks to a detail page (PeopleProfile template, or PeopleProfile.master).

  • Charles Afford 1163 posts 1709 karma points
    Nov 14, 2014 @ 20:51
    Charles Afford
    0

    Ok in your structure what nodes are peopleProfile? What is the document type of peopleProfile.

    In the code above is the file .aspx or .cshtml?

    Charlie

  • Scott Ritshie 35 posts 100 karma points
    Nov 14, 2014 @ 20:52
    Scott Ritshie
    0

    I must not be explaining this very clearly.

    From the most basic level, is there an example set of code somewhere that you can point me to that shows, step by step, how to use masterpages to display partial views or macros that pull the child nodes (remember, I use these to display data) of only 1 particular parent node?

    This is an item detail page for a person's profile.

  • Scott Ritshie 35 posts 100 karma points
    Nov 14, 2014 @ 20:53
    Scott Ritshie
    0

    I must not be explaining this very clearly.

    From the most basic level, is there an example set of code somewhere that you can point me to that shows, step by step, how to use masterpages to display partial views or macros that pull the child nodes (remember, I use these to display data) of only 1 particular parent node?

    This is an item detail page for a person's profile.

  • Scott Ritshie 35 posts 100 karma points
    Nov 14, 2014 @ 20:55
    Scott Ritshie
    0

    Nodes called "PersonProfile" are the parent nodes.

    Can you kindly look at my code? 

  • Charles Afford 1163 posts 1709 karma points
    Nov 14, 2014 @ 20:57
    Charles Afford
    0

    Hi Its not that explaining well i am trying but i need answers to the questions i ask so i can help :)

    Like are you using MVC or WebForms (sounds like you are using both)

    What are the document types? Do you know what they are?

    Charlie

  • Charles Afford 1163 posts 1709 karma points
    Nov 14, 2014 @ 20:58
    Charles Afford
    0

    Sure i will take a look for you

Please Sign in or register to post replies

Write your reply to:

Draft