Creating a widget without Content Part and using Controller

Topics: Customizing Orchard, Writing modules
Sep 2, 2011 at 3:06 AM

Hi all,

I'm looking to build a form widget that posts the data to a controller action. Seems simple enough but I'm struggling. I don't want the widget to have a content part as it doesn't need it. All the example I've seen contains ContentParts and I've actually built my own twitter widget a content part so I understand how it all works.

I maybe even talking rubbish, so let me explain. I don't want to save anything in the database, I just want to render a widget that contains a shape template (which contains the form) and then submit to a custom action on a controller within my module. This action will then submit the form data to campaign monitor and so I don't need to save the information in the database.

I'm building a widget as the form will be shown on all pages, in a specific zone.

I know how to create the controller and action and register a route with an area for the controller in my module.

Can I create a seperate view model and use for my shape template or do I need to use a content part, which acts as the view model but I just don't have a handler that then submits the data?

Sorry if a bit confusing question. If anyone has some real world examples of doing this that would be great.

Thanks,

Kevin

Sep 2, 2011 at 10:28 AM
Edited Sep 2, 2011 at 10:31 AM

You can create a content part without creating a content part record. Be mindful though that it's not unusual to have configuration settings a widget, and you may want to add them at a later date.

Usually, you would create the new content part like this:

public class MyPart : ContentPart<MyPartRecord>
{ }

 but Orchard is happy enough if you just derive from ContentPart rather than ContentPart<MyPartRecord>. There is an example of this in the search widget (take a look at Orchard.Web/Modules/Orchard.Search/Models/SearchFormPart.cs).

You will still need to create a handler and a driver as you would with any other content part (see Writing a Content Part), although clearly you will need to remove the handler code that asks for an IRepository<> and sets the storage filter (since MyPartRecord does not exist).

The rest of it is pretty much just the same as writing a regular content part/widget - you'll need to display the correct mark-up to display the form and let the user submit it to your controller, and maybe work out which content item or URL the request came from so you can return the user back there once your controller method has finished.

Hope this helps!

Sep 2, 2011 at 1:28 PM

Yes, you should be able to do this, and @kobowi has a great answer, especially looking at the SearchForm widget. Just to add onto his answer, and bring emphasis to your question "Can I create a seperate view model and use for my shape template or do I need to use a content part, which acts as the view model but I just don't have a handler that then submits the data?" ...

Yes, you'll see that's basically exactly what the SearchForm does. You can create a Driver for your ContentPart (which as @kobowi pointed out can just be a simple ContentPart with no backing record if you want) and the Driver can contain just a Display method. It doesn't need to contain the Editor methods. And the Display method can return whatever datamodel you want for the shape. It can just rely on dynamic properties which contain the values, or it can have one property which itself is a strongly typed view model that your view can reference. Either way will work, but regardless you can have the form in your shape template post to your own controller and do whatever you want from there.

Sep 2, 2011 at 1:49 PM
Hi guys,

Thanks for the quick and thorough replies I really appreciate it. I will give that a go and see how I go.

I do have another question about accessing content type records from within a controller but I'll post that as a new discussion.

Thanks for the help,

Kev