Copied to clipboard

Flag this post as spam?

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


  • Aron 7 posts 107 karma points
    Jun 15, 2016 @ 11:24
    Aron
    0

    Creating a search-form in umbraco using a surface controller

    Hello!

    I'm currently trying to implement a simple search page in umbraco using a surface controller. What I've got so far is as follows:

    • View (created from the umbraco backoffice with a doc type and template)

      • The view calls Html.Action("GetSearchForm", "MyController") which in turn returns a partialview (_SearchForm) containing the form.
      • _SearchForm should render _SearchResults, that binds to _SearchForms Model.Results @Html.Partial("_SearchResults, Model.Results

      • I'm currently using Html.BeginUmbracoForm("FormSubmit"...) with FormMethod set to post.

    Posting to the action works fine, but when I've retrieved the results from the database (not the umbraco database) I want to return the partialview updated with the results, my controller looks something like this:

        [HttpPost]
        public ActionResult FormSubmit(SearchModel model)
        {
              if(!ModelState.IsValid)
              {
                  return CurrentUmbracoPage();
              }
    
              //Results is a List of type Result
              model.Results =  GetResults();
    
              return PartialView("_SearchForm", model);
        }
    

    When I do this I end up losing the layout of the page, Sure, i get the partial-view rendered, and the results are there, but it's obviously wrong.

    What am i doing wrong? As of now, i've resorted to putting the results into

    TempData["Results"] = GetResults(); return CurrentUmbracoPage();

    but it's a solution that i find repulsive.

  • Jinesh Kaneriya 22 posts 155 karma points
    Jun 15, 2016 @ 13:19
    Jinesh Kaneriya
    0

    Use ajax call to controller via javascript or jquery like

    $.ajax({
                    url: "/umbraco/surface/contact/submit",
                    type: 'post',
                    dataType: 'json',
                    data: contactdata,
                    async: true,
                    success: function (response) {                  
                    },
                    error: function (xhr, status, error) {
                        alert(error);
                    }
                });
    

    In ajax's success function do DOM manipulation and add search content.

  • Aron 7 posts 107 karma points
    Jun 15, 2016 @ 13:31
    Aron
    100

    That is true, however i think i managed to fix it.

    My main view now has the following:

    ViewCreatedInUmbracoBackOffice.cshtml =

    @inherits UmbracoViewPage<MyModel>
    @{
         Layout = "Layout.cshtml";
         var currentPage = Umbraco.Content(umbraco.NodeFactory.Node.GetCurrent().Id);
     }
    
     @Html.Partial("_SearchForm", model)
     @Html.Partial("_SearchResults", model.Results)
    

    I've also set up a controller that inherits from RenderMvcController, and i've overridden the index

    //Returning the view does not trigger this as far as i can see. It only fires on the first request of the page, which is why i'd say its safe to return a new MyModel()
    public override ActionResult Index(RenderModel model)
    {
        //Please note that MyModel must inherit from RenderModel.
        return base.Index(new MyModel());
    }
    

    ! Please read the following

    The controller inheriting from RenderMvcController should be named exactly as the view that was created in the backoffice


    I've created a separate controller that inherits from SurfaceController, this now handles the httppost.

    In my FormSubmit i can now do as follows:

    [HttpPost]
    public ActionResult FormSubmit(SearchModel model)
    {
          if(!ModelState.IsValid)
          {
              return CurrentUmbracoPage();
          }
    
          //Results is a List of type Result
          model.Results =  GetResults();
    
          return View("ViewCreatedInUmbracoBackOffice", model):
    }
    

    All of this allows me to still use the CurrentPage.GetGridHtml(), and the Umbraco.GetDictionaryValue(), which is super handy.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies