MuriloKunze
MuriloKunze

Reputation: 15563

Return to the previous page

I have a search text input in my layout page in a partialView and other 3 pages that uses layout:

@using (Html.BeginForm("Search", "Product"))
{
    @Html.TextBoxFor(m => m.SearchText)
    @Html.ValidationMessageFor(m => m.SearchText)

    <input type="submit" value="Search"/>
}

When it goes inside Product/Search action, how can I know where it came from and return to the correct page with a message assuming the 3 other pages uses different ViewModels?

Upvotes: 2

Views: 1293

Answers (1)

Ethan Brown
Ethan Brown

Reputation: 27282

In your layout, modify your BeginForm to include the request path in the route values:

@using (Html.BeginForm("Search", "Product"),new {path = Request.Path})

Then, in your controller, you can finish up by redirecting to that path:

public ActionResult Search( string path, FormCollection form ) {
    // build your search results here
    return Redirect( path );
}

And that should do the trick. If you need to pass additional information along, you can just append a query string to the path:

public ActionResult Search( string path, FormCollection form ) {
    // build your search results here
    return Redirect( path + "?message=foo" );
}

Upvotes: 2

Related Questions