ASP.NET MVC URL Routing problem
i have defined a route as below:
context.MapRoute("SearchEngineWebSearch", "search/web/{query}/{index}/{size}",
new
{
controller = "search",
action = "web",
query = "",
index = 0,
size = 5
开发者_运维百科 });
and action method to handle request match with that:
public System.Web.Mvc.ActionResult Web(string query = "", int index = 0, int size = 5)
{
if (string.IsNullOrEmpty(query))
return RedirectToRoute("SearchEngineBasicSearch");
var search = new Search();
var results = search.PerformSearch(query, index, size);
ViewData["Query"] = query;
if (results != null && results.Count() > 0)
{
ViewData["Results"]= results;
return View("Web");
}
else return View("Not-Found");
}
and form to sent parameter to action method:
<% using (Html.BeginForm("Web", "Search", FormMethod.Post))
{ %>
<input name="query" type="text" value="<%: ViewData["Query"]%>" class="search-field" />
<input type="submit" value="Search" class="search-button" />
<input type="hidden" name="index" value="2" />
<input type="hidden" name="size" value="2" />
<%} %>
now after click on submit and sending value to action method all route values updated but url values still is equals to first time of sending parameter. for example if i sent for first time request such as http://localhost/search/web/google and for next time http://localhost/search/web/yahoo, query parameter which passed to action method is yahoo but url after postback is http://localhost/search/web/google still!
can anybody help me plz? ;)
Try something like
return RedirectToAction("Web",
new { query = query, index = index, size = size});
instead of return View("Web");
.
Also, note that you should perform a GET instead of a POST. And the index and size parameters may be ommitted if they were submitted with the default values.
精彩评论