Data from Controller to View

Topics: Writing modules
Sep 14, 2012 at 11:54 PM

Forgive me for being such a noob with orchard.  I have used the "building a hello world module" tutorial and everything works fine.  I think I may just need to somehow refresh the module but anyways...

I am trying to pass data from the controller to the view.  I can't get anything to work. (TempData, ViewData, Model)  I have even deleted the controller and the view still works which tells me it needs to be refreshed or it isn't even using the controller within Modules folder I created.

Thanks for any guidance...

Coordinator
Sep 15, 2012 at 1:11 AM

Don't worry about being a n00b ;) The easiest way to get data into your view is through a shape. Here's what I recommend...

Create a Shape property on your controller:

        dynamic Shape { get; set; }

Then, inject a shape factory from the constructor:

        public SomeController(IShapeFactory shapeFactory) {
            Shape = shapeFactory;
        }

Create a shape from your action and return an action result that sends that shape in:

        public ActionResult SomeAction(int someParameter) {
            var shape = Shape.SomeShapeName(
                SomeInteger: someParameter,
                SomeString: "Hello World"
            );

            return View((object)shape);
        }

Finally, create a template in your views folder:

The message is: @Model.SomeString and the number you asked for is: @Model.SomeInteger

Sep 17, 2012 at 4:13 PM

Thank you.  I must be missing something.  In my view, @Model.SomeString does not seem be set.  It is just an empty string.  Do you have any idea what I could be missing?

Coordinator
Sep 17, 2012 at 5:18 PM

A typo maybe? It's case-sensitive.

Sep 17, 2012 at 5:44 PM

No. I have copied your code just to test and see some results.  And I only changed the Controller constructor name and the Action name.

 

One point to make that I do not understand.  If I comment out the controller then it still runs.  If no view is returned then it shouldn't work.  It's not cached.  I have checked that as well.  That shouldn't happen so I think the problem is somewhere else. 

 

I'm just going to try and create a new module.

Coordinator
Sep 17, 2012 at 5:47 PM

Oh, I may have an idea. Try to return a ShapeResult instead.

Sep 17, 2012 at 5:56 PM

Yeah, I already tried that after doing some research.  I'll attempt the new module and see where that gets me.  Thanks for the help.

Coordinator
Sep 17, 2012 at 6:24 PM

Please keep us updated.

Sep 17, 2012 at 6:42 PM

I think my problem may have been permissions because when I created the new Module I had to change the permissions to generate it.

Okay, I am getting somewhere now.  I have an error "Shape type SomeShapeName not found".  Where is it looking for SomeShapeName?  The code you gave me is using it in the Action but I have no clue how that works or where it is looking for SomeShapeName.  Thanks.

 

Coordinator
Sep 17, 2012 at 8:08 PM

What it's telling you is that you need to create the template for that shape. That was the last step I gave you: in your Views folder, create a file named SomeShapeName.cshtml and paste in there the last bit of code I gave.

Sep 17, 2012 at 8:20 PM

Cool.  I didn't understand that or read it well I guess.  Thanks for all your help.