我有一个文本框,我将数据输入并通过帖子传递给我的'主页'控制器“动作'结果'
我希望当我发回
时,网址最终会像这样https://localhost:44301/Home/Results/San Francisco, CA, United States
我正在传递这样的文本框数据。
@using (Html.BeginForm("Results", "Home", FormMethod.Get, new { @class = "navbar-form navbar-left", role = "search" }))
{
<div class="form-group">
<input type="text" class="form-control" placeholder="Search" id="navbarautocomplete" name="location">
<button type="submit" class="btn btn-default">Submit</button>
</div>
}
这是我的路线。
routes.MapRoute("SearchResults",
"home/results/{location}",
new { controller = "Home", action = "Results", location = ""}
);
如何设置我的路由或表单,以查看我在网址中提交的位置数据?
我可以看起来像这样。
https://localhost:44301/home/results?location=San+Francisco%2C+CA%2C+United+States
但在/ results /
之后我想要旧金山答案 0 :(得分:2)
正如@StephenMuecke在评论中提到的那样,您可以将搜索值发布到(单独的)操作,然后重定向到结果页面,将该位置作为参数传递:
@using (Html.BeginForm("Search", "Home", FormMethod.Post, new { @class = "navbar-form navbar-left", role = "search" }))
{
<div class="form-group">
<input type="text" class="form-control" placeholder="Search" id="navbarautocomplete" name="location">
<button type="submit" class="btn btn-default">Submit</button>
</div>
}
然后在你的控制器中:
[HttpPost]
public ActionResult Search(string location)
{
return RedirectToAction("Results", new { location = location });
}
public ActionResult Results(string location)
{
return Content("location is: " + location);
}
您还要在RouteConfig中设置以下路线以获取友好的URL(确保它高于默认路线,因为它们与自上而下匹配)。
routes.MapRoute(
name: "SearchResults",
url: "Home/Results/{location}",
defaults: new { controller = "Home", action = "Results" }
);