Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • andrew shearer 512 posts 661 karma points
    Aug 16, 2013 @ 05:23
    andrew shearer
    0

    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

     

  • andrew shearer 512 posts 661 karma points
    Aug 17, 2013 @ 02:32
    andrew shearer
    0

    .. 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..

  • Charles Afford 1163 posts 1709 karma points
    Aug 17, 2013 @ 12:20
    Charles Afford
    0

    For the GET request take a look at a look at route hijacking :).  i think this is what you are after :D 

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Aug 17, 2013 @ 23:13
    David Brendel
    0

    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

  • andrew shearer 512 posts 661 karma points
    Aug 18, 2013 @ 07:44
    andrew shearer
    0

    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):

    1. wire up a custom route i.e.

              //Create a custom route
          RouteTable.Routes.MapRoute(
              "Cat Search Route",
              "catsearch/{action}/{term}",
              new
              {
                  controller = "MyCatSearch",
                  action = "Find",
                  term = UrlParameter.Optional
              });
      
    2. add controller action plumbing using "UmbracoController" derivative:

      public class MyCatSearchController : UmbracoController
      

      { public MyCatSearchController() : this(UmbracoContext.Current) {} public MyCatSearchController(UmbracoContext umbracoContext) : base(umbracoContext) {}

      [System.Web.Http.HttpGet]
      public ActionResult Find(CatSearchViewModel searchViewModel)
      {
          var searchResultsListing = Umbraco.TypedContent(1083); //1083 = 'search listing' content node in umbraco
      
      
      
      return View("SearchResults", CreateRenderModel(searchResultsListing));
      
      } 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.

  • andrew shearer 512 posts 661 karma points
    Aug 18, 2013 @ 08:05
    andrew shearer
    0

    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.

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    Aug 27, 2013 @ 04:41
    Shannon Deminick
    0

    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.

  • andrew shearer 512 posts 661 karma points
    Aug 30, 2013 @ 02:21
    andrew shearer
    0

    thanks Shannon.

    issue logged here http://issues.umbraco.org/issue/U4-2748

Please Sign in or register to post replies

Write your reply to:

Draft