Custom Content Type - The easiest way to display data

Topics: Customizing Orchard
Dec 2, 2014 at 8:50 PM
I'm trying to figure out the easiest way to display the custom content type using the *.cshtml (razor) template file.

Let's imagine I have a content type Gallery Item which consists of few fields and I want to use ONLY razor to display it. Is it possible?

Can I query the database from the View? any examples?

I want to create a custom logic with some conditionals and I do not want to create a module. Is it doable?
Dec 2, 2014 at 9:00 PM
The easiest way is to simply create your custom content type and display it using the default Razor files.
You can override these files from your theme if you want to. No custom module necessary for that.
You can include logic and query the database from your Razor files.
So yes, it's doable, but it is definitely considered a bad practice.
I recommend creating a module that contains your custom logic.
Depending on your scenario, you could implement a shape table provider, a driver, a filter, etc.
Dec 2, 2014 at 9:09 PM
Thank you for a quick response!

I know it's a bad practice but let's focus on this task.

Let's assume I've created 10-20 different entries of the custom content type and I want to create a widget to display them all, basically to generate the HTML code.
Requirement is to NOT use modules , would that be possible?

I could not find any examples of the direct data access from the view files, does it mean it's not possible?

I know I could use projections but my case is too complex for that, I need to use cshtml template. Any ideas?

The shape tracking module is enabled but it doesn't show the alternates, models what could be the reason for that?
Dec 2, 2014 at 10:12 PM
Edited Dec 2, 2014 at 10:12 PM
kbugala wrote:
I could not find any examples of the direct data access from the view files, does it mean it's not possible?
No, it simply means it is bad practice and no one is doing it. ;)

Don't tell anyone I told you this, but you can resolve an instance of IContentManager in your view, and use it to query your content items, like this:
@using Orchard.ContentManagement
   var contentManager = WorkContext.Resolve<IContentManager>();
   var contentItems = contentManager.Query("MyContentType").List();
I don't know why the shape tracer isn't showing alternates. Might be a bug. Or maybe you didn't enable any feature that provides alternates, such as "UrlAlternates".
Dec 2, 2014 at 10:39 PM
Wow, so I'll be the first one doing it :) thanks a million , that's what I needed.

I do understand the arguments behind the separation of concerns but in some cases simplicity is a key ,imho.

If you could also show me an example of accessing a field property that would be awesome (Text Field or Media Library Picker Field)

The URL Alternates module is enabled too, the shape tracing tool used to display much more information but now it's kind of empty, it misses the models, shapes, alternates etc.. can't even see the button to create an alternate
Dec 3, 2014 at 11:23 AM
getting there ....
var item_category = ((ContentItem)item).Parts.SelectMany(p => p.Fields).Where(f => f.Name == "Category").First().Storage.Get<string>(null);
the question now is how to do the same with the Media Gallery Field which allows multiple items...anyone? All I need is the get a list of media URLs