my scenario is a search form in the header of my site i.e. on every page. I also have a content node + template/view for displaying search results for the specified term. What is the best way for my search form to request this umbraco page and supply it with the term as the viewmodel?
I've been looking through all of the surfaceController examples in the docs section and what I've found online, but they're all about POST actions to a form sitting on the current page, which is slightly different from what I want to achieve.
.. I was also thinking about the option of a custom route, but there are elements on the search results listing template that should be content-managed, so I don't want to lose umbraco context, therefore sticking with an UmbracoForm still sounds like the right track to be going down..
Maybe its possible to do the form posting to a surface controller and than handle the search there and then redirect to the search page with the results as the model.
Or maybe you can post the form to ~/search/ and on the search oage calling a surface controller with the query string parameter with your search term.
Something like @Html.Action("SearchResults","SearchSurface", new { term = Request["term"] }
thanks for the pointers Charles and David :)
@David - i had considered that method but i have two objectives i really want to achieve:
a) GET request rather than POST
b) aspects of the search results listing are content-managed via the CMS
}
private SearchResultsModel CreateRenderModel(IPublishedContent content)
{
var model = new RenderModel(content, CultureInfo.CurrentUICulture);
//add an umbraco data token so the umbraco view engine executes
RouteData.DataTokens["umbraco"] = model;
return new SearchResultsModel(baseModel: model)
{
Matches = new[] { "Ragdoll", "Persian", "Siamese", "Bengal", "Scottish Fold" },
};
}
}
bit of hard wiring there but its only a proof on concept. The "Find" action obtains the CMS item that acts as the basis for the rendered umbraco view and also uses a custom model to pass this content item along with the search results.
Hopefully this info helps someone else and/or someone can critique and suggest improvements?
The outstanding issue for me is that I now have a “Search Results” item in the content tree primarily for defining the corresponding umbraco view that my custom route will use. This content item has a Url that will throw an exception if accessed because the view only works in the context of my custom route and view model. Probably not a big deal though really.
We should build in Get into BeginUmbracoForm, if you wanna log an issue on the tracker and assign to me I'll take care of it.
In the meantime another way you could acheive this without any custom routes would be to just create a regular old html <form with an action of the Umbraco page you want to submit to with the query strings you want to append.
Then you can just hijack the route for the page your are submitting to to retreive your get parameters, I'd assume that model binding would work in that context too so you 'should' be able to use your strongly typed model CatSearchViewModelBut I've not tested this.
HttpGet form actions in Umbraco MVC
are these possible?
my scenario is a search form in the header of my site i.e. on every page. I also have a content node + template/view for displaying search results for the specified term. What is the best way for my search form to request this umbraco page and supply it with the term as the viewmodel?
I've been looking through all of the surfaceController examples in the docs section and what I've found online, but they're all about POST actions to a form sitting on the current page, which is slightly different from what I want to achieve.
thanks
.. I was also thinking about the option of a custom route, but there are elements on the search results listing template that should be content-managed, so I don't want to lose umbraco context, therefore sticking with an UmbracoForm still sounds like the right track to be going down..
For the GET request take a look at a look at route hijacking :). i think this is what you are after :D
Maybe its possible to do the form posting to a surface controller and than handle the search there and then redirect to the search page with the results as the model.
Or maybe you can post the form to ~/search/ and on the search oage calling a surface controller with the query string parameter with your search term.
Something like @Html.Action("SearchResults","SearchSurface", new { term = Request["term"] }
Hope i could help
thanks for the pointers Charles and David :) @David - i had considered that method but i have two objectives i really want to achieve: a) GET request rather than POST b) aspects of the search results listing are content-managed via the CMS
My first cut is as follows (based on a post from Shannon http://shazwazza.com/post/Custom-MVC-routing-in-Umbraco):
wire up a custom route i.e.
add controller action plumbing using "UmbracoController" derivative:
{ public MyCatSearchController() : this(UmbracoContext.Current) {} public MyCatSearchController(UmbracoContext umbracoContext) : base(umbracoContext) {}
}
bit of hard wiring there but its only a proof on concept. The "Find" action obtains the CMS item that acts as the basis for the rendered umbraco view and also uses a custom model to pass this content item along with the search results.
Hopefully this info helps someone else and/or someone can critique and suggest improvements?
The outstanding issue for me is that I now have a “Search Results” item in the content tree primarily for defining the corresponding umbraco view that my custom route will use. This content item has a Url that will throw an exception if accessed because the view only works in the context of my custom route and view model. Probably not a big deal though really.
We should build in Get into BeginUmbracoForm, if you wanna log an issue on the tracker and assign to me I'll take care of it.
In the meantime another way you could acheive this without any custom routes would be to just create a regular old html <form with an action of the Umbraco page you want to submit to with the query strings you want to append.
Then you can just hijack the route for the page your are submitting to to retreive your get parameters, I'd assume that model binding would work in that context too so you 'should' be able to use your strongly typed model CatSearchViewModelBut I've not tested this.
thanks Shannon.
issue logged here http://issues.umbraco.org/issue/U4-2748
is working on a reply...