Copied to clipboard

Flag this post as spam?

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


  • Lasse Kofoed 49 posts 177 karma points
    Aug 14, 2014 @ 11:10
    Lasse Kofoed
    0

    MVC "onpageload"

    Hello there, this might be a novice question. But here we go anyway.

    The searchpage works as intented, but when the searchQuery is submitted, I need the the controller to do the search "onpageload" if the querystring contains the "SearchWord" - right now, the user needs to click the search button to activate the controller.

    I have the following files

    \Views\MacroPartials\search_input

    ---------------


    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    <div class="search-input">
        <div class="search-title hidden-xs">
            <span>Velkommen!</span> - Hvad kan vi hjælpe med i dag ?
        </div>
        <form action="/soegning.aspx" method="GET">
            <div class="search-field">
                <input type="submit" value="Søg" />
                <input type="text" name="SearchWord" id="SearchWord" placeholder="Hvad kan vi hjælpe med?" />
            </div>
        </form>
    </div>


    \Views\MacroPartials\PageSearch

    ---------------

    @model sosusj.webapplication.Models.PageSearchModel
    @{
        if (!string.IsNullOrWhiteSpace(Request["SearchWord"]))
        {
            Model.SearchWord = Request["SearchWord"].ToString();
            
        }
    }
    @using (Html.BeginUmbracoForm<PageSearchController>("SearchPage"))
    {
        <div class="row">
            <div class="col-xs-12">
                @*                <label for="SearchWord">Søgeord</label>*@
                @Html.TextBoxFor(x => x.SearchWord, new {@class = "form-control"})
            </div>
            <div class="col-xs-12">
                @Html.ValidationSummary()
            </div>
            <div class="col-xs-12">
                <input type="submit" value="Søg" class="form-control" />
            </div>
        </div>
        <div class="row pagesearch">
            @if (TempData["message"] != null)
            {
                <div class="col-xs-12 ">
                    <p>@Html.Raw(TempData["message"].ToString())</p>
                </div>
            }
            @if (TempData["Result"] != null)
            {
                List<IPublishedContent> result = (List<IPublishedContent>)TempData["Result"];
                <div class="col-xs-12 ">
                    <br/>
                    Din søgning på <strong>@Request.QueryString["mainquery"]</strong> returnerede @result.Count() sider
                </div>
                foreach (IPublishedContent pages in result)
                {
                    <div class="col-xs-12 result ellipsis">
                        <a class="title" href="@pages.Url">@pages.Name</a>
                        @pages.GetPropertyValue("bodyText")
                       </div>
                }
            }
        </div>

    \Views\SoegSide

    ----------

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = "base_text.cshtml";
    }
    @Html.Partial("PageSearch", new PageSearchModel())


    \Models\PageSearchModel

    ---------------

        public class PageSearchModel
        {
            [DisplayName("Søgeord")]
            public string SearchWord { get; set; }
        }
    }


    \Controllers\PageSearchController

    -------------------

      public class PageSearchController : SurfaceController
        {
            private readonly BaseSearchProvider Searcher = ExamineManager.Instance.SearchProviderCollection["InternalSearcher"];
            [HttpPost]
            public ActionResult SearchPage(PageSearchModel model)
            {
                if (base.ModelState.IsValid)
                {
                    if (string.IsNullOrWhiteSpace(model.SearchWord.ToString()))
                    {
                        return RedirectToCurrentUmbracoPage();
                    }
                    var homeNode = CurrentPage.AncestorOrSelf("home");
                    List<IPublishedContent> filtredResults = new List<IPublishedContent>();
                    foreach (IPublishedContent result in Umbraco.TypedSearch(model.SearchWord.ToLower(), true, "InternalSearcher"))
                    {
                        var resultHome = result.AncestorOrSelf("home");
                        if (resultHome.Id == homeNode.Id)
                        {
                            filtredResults.Add(result);
                        }
                    }
                    TempData.Add("Result", filtredResults);
                    return CurrentUmbracoPage();
                }
                return RedirectToCurrentUmbracoPage();
            }
        }
  • Bogdan 250 posts 427 karma points
    Aug 14, 2014 @ 13:32
    Bogdan
    0

    Hi Lasse,

    I don't think you can achieve that with your approach, because the SearchPage action doesn't execute unless the form is POSTed.

    You can simplify everything by putting the code from the SearchPage action in the SoegSide view, where you can check for Request["SearchWord"]. Not that elegant, but it works well. You won't need a PageSearchModel anymore, and the PageSearch partial is not necessarily needed, you can have everything in the SoegSide view.

    Alternatively, you can implement a custom controller that hijacks the Umbraco route for the search page.

  • Charles Afford 1163 posts 1709 karma points
    Aug 17, 2014 @ 22:29
    Charles Afford
    0

    Hi Lasse,

    I think what you need to do is hijack the route of your doctype  take a look here 

    http://our.umbraco.org/documentation/Reference/Mvc/custom-controllers

    If you dont need this then call the method in your view, set the result in a model property and then pass that to your partial view?

    Make sense?

    Charlie :)

     

Please Sign in or register to post replies

Write your reply to:

Draft