This project is read-only.

Building a search form on the display of a part

Topics: Writing modules
Sep 27, 2011 at 3:34 PM

We have a content type that we want to be searchable in the front-end, so my approach was to create a widget that has an empty part record model and have the display method of that part's driver show a search form that would query the content types via ajax/json from a controller action.

First things first, is there a better way to handle this situation within Orchard, or is this standard practice for front-side display-facing forms?

Second, if it is the way to handle it, I'm having difficulty getting my SelectListItems passed into my display view to render a's complaining about the objects being dynamic and therefore they aren't able to be used in DropdownListFor() or DropdownList().

I'd love to have a discussion on this so I know I'm going down the best path possible within Orchard...

Sep 27, 2011 at 3:55 PM

As for the dynamic stuff, I was able to get it to work by:

 subCategorySelectList = subCategories.Select(s => new SelectListItem
                Selected = s.Id == part.SubCategoryId,
                Text = s.CategoryRecord.Name + " - " + s.Name,
                Value = s.Id.ToString()

@{var subCategories = new SelectList(Model.SubCategories);}
@Html.DropDownList("SubCategoryId", subCategories, "Choose a category...")

Sep 28, 2011 at 12:08 AM

If it's complaining about the Model being dynamic  you just need to strongly typed it at the beginning of your view.

The same thing is currently also bothering me ... if you have a  " front-side display-facing form" that is part of a content type where do you handle it's POST Actions...I don't think the part's driver is the right place?

Sep 28, 2011 at 12:14 AM

How is Orchard.Comments doing it?

Sep 28, 2011 at 7:04 AM
Edited Sep 28, 2011 at 7:07 AM
using (Html.BeginForm("Create", "Comment", new { area = "Orchard.Comments" }, FormMethod.Post, new { @class = "comment-form" }))
And don't forget Orchard's antiforgery token :)
Sep 28, 2011 at 2:31 PM

I think my scenario is different than the comments.  For the comments, you're attaching the form to a content type (page, blog post, etc).   In my scenario, it doesn't attach to a content part.  It doesn't attach to anything, really. It's just a page that you browse to that searches for something in the CMS.  So, I decided to make it a widget and just attach it to a page by a URL Rule so that it only shows up when you're on my search page.

I also thought about just using a controller, but then I feel like I would lose my left/right zones where we put sidebar content and everything...

Sep 28, 2011 at 5:57 PM

no, if your controller action returns a shape result, or simply is themed, you won't lose anything.

Sep 28, 2011 at 6:08 PM

I'm not quite sure how that'd work.  Normally, we create a page through the CMS, select the layout (say, 2-column with sidebar) and then maybe a part that renders as part of a zone, say the content zone.

If we have a controller action, doesn't the contrller server up the whole page? Even if you return a shaperesult, how do you tell the controller to only render its action/shaperesult into say, the content zone?

Sep 28, 2011 at 6:16 PM

Yes, but now you're confusing me. You just said you didn't want to tie to a content type. Can you explain again what you are trying to do?