This project is read-only.

General Guide for Access to items in Razor View? BlogPostURL and Linking to it from an image.

Topics: Customizing Orchard, Writing themes
Dec 10, 2011 at 9:36 PM

Okay, I have a customized theme and so far it's been more around the layout and css customizations.  Now however, I'm making some changes that are very specific to an area of the site.


My BlogPost has an Image field added to it and my BlogList and BlogPost both display it.  On another area of the site, the user would like to have an image only (no text).  I have completed that using the ShapeTracer and creating Alternate Views to disable the text, make the image larger etc.

However they have one more simple request.... the image should link to the BlogPost just like the title and the more button did.

So I have a new View that is specific to this area of the site and specific to the image itself.


The markup is simple:

<div class="image-field" style="float:left; margin: 0px 15px 15px 0px">  
<img src="@Url.Content(Model.ContentField.FileName)" alt="@Model.ContentField.AlternateText" width="@Model.ContentField.Width" height="@Model.ContentField.Height"/>
I attempted to look at the BlogTitle and <more> tag for guidance but I'm not really comfortable getting new items out of the Model object.  Can someone teach me to fish and help me with this basic need?  Should I instead be adding the link to the post outside the Image Field?

Dec 11, 2011 at 12:42 AM
Edited Dec 11, 2011 at 12:43 AM

When focused on your image's shape, in shape tracing, you'll want to go into the Model tab and drill down in there. It should be something like @Model.ContentItem.RoutePart.Path (verify, I'm typing that from the top of my head).

Dec 11, 2011 at 1:13 AM

Apparently your memory (and experience with orchard) pay off.

@Model.ContentPart.ContentItem.RoutePart.Path it is!

Now, the most important part.  *Why* could I find it there?  That is, what does this structure represent?

As I focus on other elements that I suspect are siblings of the ImageField that path appears to be static for all of those elements.

Thanks for teaching me how to fish!

Dec 11, 2011 at 1:23 AM

Model is the shape that the template is rendering. What is on the shape object was put there (typically) by a part driver. Most drivers put a property called ContentItem there, or ContentPart, pointing to the item or part that the shape was built from. ContentItem itself is a dynamic object that can return any of its content part (RoutePart here), and then the part itself exposes its properties (Path). Does this help?