SaaS CMS has officially launched! Learn more now.

HTTP Post across pages in MVC

Vote:
 

I want to implement EPiServer search, where the top of each page will have a "search box", defined in our _Layout.cshtml similar to:

 

@using (Html.BeginForm("Results", "Search"))
{
@Html.TextBox("keywords")
@Html.Button("Search", "Search")
}

I have a page instance of type SearchPage set up and named "Search", but unless the code above is posted from the *same controller* (the index action of the Search page) the HttpPost fails with a 404 not found.  If I'm already on the "Search" controller, the form posts just fine, and things work as I expected.

Perhaps even more interestingly, if I rename the "Results" action to "Index" (leaving the [HttpPost] attribute on it), the HttpPost decorated method is called when I'm already on the search page, but if I submit the same form from any other page, the HttpGet version of the index is called.

I have a requirement to show a search box in the header of every page.  How do I cross POST forms from different pages in EPiServer?

#71444
May 17, 2013 21:01
Vote:
 

Hi,

There's a working example of such a search box in the MVC version of the Alloy templates.

The gist of it is that you want to route to the PageData object that is your search page and not directly to it's controller. To do that you can use an overload of BeginForm and pass in a RouteValueDictionary with the search page's route values. You can retrieve those in a number of ways, but here's one (intented to be put in a static class somewhere and used as an extension method for RequestContext):

public static RouteValueDictionary GetPageRoute(this RequestContext requestContext, PageReference pageLink, RouteValueDictionary routeValues = null)
{
    var values = routeValues ?? new RouteValueDictionary();
    values[RoutingConstants.NodeKey] = pageLink;
    values[RoutingConstants.LanguageKey] = ContentLanguage.PreferredCulture.Name;
    if (requestContext.HttpContext != null)
    {
        var idkeep = requestContext.HttpContext.Request.QueryString["idkeep"];
        if (idkeep != null)
        {
            values["id"] = pageLink.ToString();
        }
    }
    return values;
}

Then create the form like:

Html.BeginForm(null, null, searchPageRouteValues)

        

#71448
May 18, 2013 14:15
Vote:
 

When I used the html helper, if the RouteValue is empty, it throws an exception, error looks like "stringToEscape is null xxxx"

this my usage @Html.BeginForm("Auth", "LoginPage", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new {}){//My authentication logic}

#71449
May 18, 2013 15:26
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.