November Happy Hour will be moved to Thursday December 5th.

Getting currentPage after FormPost from _Layout-file.

Vote:
 

Hello, I'm trying to build a custom SearchForm that is called in my _Layout.cshtml file, but the problem is when I redirect the searchResult to a SearchPage it doesn't get a currentPage, and Joel Abrahamssons NavigationHelper.cs method RenderMainNavigation() crashes because currentPage is null (even though the page isn't displayed in navigation).

Here's my code in the controller:

 public ActionResult Search(SearchPage currentPage) { var model = new SearchPageViewModel(currentPage); return View(); } [HttpPost] public ActionResult Search(string searchField) { var model = new SearchPageViewModel(); model = ConnectionHelper.Search(searchField); return View("Search", model); }

 

Simple form in _Layout.cshtml:

@using (Html.BeginForm("Search", "SearchPage", FormMethod.Post)) { @Html.TextBox("searchField")  }


Any idea of how I can get a SearchPage as currentPage in the controller?
I've tried changing the return type to RedirectToAction and putting the node as a pageReference I chose in the StartPage, but that doesn't work as the model and the node mix, making the model in the view mismatch and crash.

#109717
Oct 13, 2014 15:45
Vote:
 

Not sure what happened with my question, but here it is again:

Hello, I'm trying to build a custom SearchForm that is called in my _Layout.cshtml file, but the problem is when I redirect the searchResult to a SearchPage it doesn't get a currentPage, and Joel Abrahamssons NavigationHelper.cs method RenderMainNavigation() crashes because currentPage is null (even though the page isn't displayed in navigation).

Here's my code in the controller:

        public ActionResult Search(SearchPage currentPage)
        {
            var model = new SearchPageViewModel(currentPage);
            return View();
        }

        [HttpPost]
        public ActionResult Search(string searchField)
        {
            var model = new SearchPageViewModel();
            model = ConnectionHelper.Search(searchField);

            return View("Search", model);
        }

Simple form in _Layout.cshtml:

                                        @using (Html.BeginForm("Search", "SearchPage", FormMethod.Post))
                                        {
                                            @Html.TextBox("searchField")
                                            <button type="submit" class="btn searchButton">Sök</button>
                                        }



Any idea of how I can get a SearchPage as currentPage in the controller?
I've tried changing the return type to RedirectToAction and putting the node as a pageReference I chose in the StartPage, but that doesn't work as the model and the node mix, making the model in the view mismatch and crash.

#109718
Oct 13, 2014 15:51
Vote:
 

Not sure if this is done correctly, but I solved it by changing it to this:

        public ActionResult Index(SearchPage currentPage)
        {
            SearchPageViewModel lists = (SearchPageViewModel)Session["Model"];
            Session.Clear();
            var model = new SearchPageViewModel(currentPage);
            model.eventResults = lists.eventResults;
            model.userResults = lists.userResults;
            model.SearchQuery = lists.SearchQuery;

            return View(model);
        }

        [HttpPost]
        public ActionResult Search(string searchField)
        {
            var model = new SearchPageViewModel();
            var page = new SearchPage();
            var pageReference = page.StartPage.SearchPageUrl;
            model = ConnectionHelper.Search(searchField);
            model.SearchQuery = searchField;
            Session["Model"] = model;

            return RedirectToAction("Index", pageReference);
        }

And the variable pageReference is a PageReference I set in my StartPage that points toward the searchPage.

#109752
Oct 14, 2014 12:23
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.