Pass data from driver to view

Topics: Writing modules
Jul 2, 2012 at 7:59 AM

My driver does everything in standard way, I don't want to implement separate ViewModel. I just need to pass SelectList that contains list of currently created blogs in order to allow user select blog from the list.

Is there ANY obvious thing in Orchard? Every common task takes hours of googling and researching code to understand implementation :))

Coordinator
Jul 2, 2012 at 8:06 AM

Yes, that one is pretty obvious actually: the shapes that you create in drivers are dynamic objects, so you can stick whatever you want on its properties. Maybe Orchard isn't the best CMS for you ;)

Jul 2, 2012 at 8:29 AM
Edited Jul 2, 2012 at 8:29 AM

As now I got this code:

protected override DriverResult Editor(MyModuleSettingsPart part, dynamic shapeHelper) {
return ContentShape("Parts_MyModuleSettings", 
() => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: part, Prefix: Prefix)).OnGroup(T("MyModule").Text); }


Where do I intend to stick my stuff? And how to get it in the view? I have tried to add it like ... Model: part, Prefix: Prefix, MyStuff: "hello world" ... but how to retrieve?

Coordinator
Jul 2, 2012 at 8:37 AM

You didn't say it was in the editor method. You should make Model be something else than the part, and have the part a property of that view model instead, and that select list another.

Jul 2, 2012 at 8:48 AM
Edited Jul 2, 2012 at 8:48 AM

It was my first idea. I made this class, say MyViewModel, and then get lost. As this class is not derived from ContentPart, I cannot derive driver like this MyDriver : ContentPartDriver<MyViewModel>

Developer
Jul 2, 2012 at 8:59 PM
Edited Jul 2, 2012 at 8:59 PM

You can create an instance of your view model inside of your Editor method, e.g.:

protected override DriverResult Editor(MyModuleSettingsPart part, dynamic shapeHelper) {

var viewModel = new MyViewModel();

return ContentShape("Parts_MyModuleSettings", () => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: viewModel, Prefix: Prefix)).OnGroup(T("MyModule").Text);
}

Then in the POST version of your editor method, you again instantiate your view model class and populate its properties via the IUpdater parameter that is passed in using the TryUpdateModel method.

 

 

Jul 3, 2012 at 5:09 AM

Thanks a lot, now it works.