我定义了如下路线:
context.MapRoute("SearchEngineWebSearch", "search/web/{query}/{index}/{size}",
new
{
controller = "search",
action = "web",
query = "",
index = 0,
size = 5
});
和处理请求匹配的操作方法:
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");
}
和表单将参数发送到操作方法:
<% 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" />
<%} %>
现在单击提交并将值发送到操作方法后,所有路由值都已更新,但 url 值仍等于第一次发送参数。例如,如果我发送了第一次请求,例如http://localhost/search/web/google和下一次 http://localhost/search/web/yahoo,则传递给操作方法的查询参数是 yahoo 但之后的 url回发仍然是 http://localhost/search/web/google!
有人可以帮我吗?;)