This project is read-only.

Accessing model properties in view

Nov 5, 2010 at 12:07 PM


I'm working with Orchard 0.8.

I've got a controller with an action called Edit. The view Edit.cshtml is defined as follows.

<h1>@Html.TitleForPage( Model.Name )</h1>

@using (Html.BeginFormAntiForgeryPost()) {



Inside the h1 tag I'd like to display the value of Model.Name. I get an error saying that there's an issue with passing dynamic properties to TitleForPage. I tried casting the property to a string, but it returns the empty string.

Besides, I'm not even sure if I'm accessing the model correctly from this view. If I use Model.Name in shapes, it works.

What am I doing wrong?

Let me know if you need further info. Unfortunately, IntelliSense doesn't help with dynamic objects and Razor views.

Nov 5, 2010 at 12:21 PM

Hello again,

if I replace Model.Name (a property defined on my content part) with Model.Title, it works.

I think I'm accessing a different object than my content part, but which one?

Nov 5, 2010 at 12:24 PM

Have you tried a statement block to initialize strongly typed variables from your dynamic model?

Like this:

@{ string name = Model.Name; }

<h1>@Html.TitleForPage( name )</h1>

Nov 5, 2010 at 12:37 PM
Yes I tried, but it doesn't work.

I think that the Model instance that I'm passing to the Display method
in Edit.cshtml is not wrapping my content part.
Nov 5, 2010 at 4:52 PM

Are you sure the Model has a Name property ? Could you share the code you used to create it ?

Nov 5, 2010 at 5:10 PM


this is how I'm creating the model:

        public ActionResult Edit(int id) {
            var evt = _eventService.Get(id);

            if (evt == null) {
                return HttpNotFound();

            var model = Services.ContentManager.BuildEditor(evt);

            return View(model);

You're right, the model doesn't have a Name property, it's my content part (referenced by evt) that has the Name property.

How can I access the content part from the IShapeProxy object returned by BuildEditor?


Nov 5, 2010 at 6:37 PM

It should be Model.ContentItem.As<YouPart>().YouPartProperty.

You need to add @using Orchard.ContentManagement;

at the top of your view to use this extension method.

Nov 5, 2010 at 7:13 PM
Ah, I see... Thanks!
Nov 6, 2010 at 11:21 AM

Hello again,

@sebastienros: Orchard complains saying that there's no method As defined in Model.ContentItem.

However, there's a method named Get that seems to do the trick:



Nov 6, 2010 at 11:36 AM

Ok, let's go ahead and take advantage of shapes.

In my controller action, I can build the view model like so:

var model = Services.ContentManager.BuildEditor(evt);

// Add a dynamic property to shape
model.EventName = evt.Name;

return View(model);

Now, in the shape template: