我有一个表单,它在提交时将所有输入字段值作为查询字符串传递。
我提交后得到的地址是:
http://localhost:51246/Markets?Keyword=keywordhere
首选地址格式为:
http://localhost:51246/Markets/keywordhere
表格的代码:
@using (Html.BeginForm("Index", "Markets", FormMethod.Get, new { id = "login", role = "form", @dataToggle = "validator", @class = "form-horizontal" }))
{
<div class="form-group">
@Html.LabelFor(x => x.Keyword, new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.TextBoxFor(x => x.Keyword, new { @class = "form-control", @placeholder = "Enter market or country name..." })
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Filter</button>
<a href="@Url.Action("Index")" class="btn btn-default">Reset</a>
</div>
</div>
}
我的路线配置是:
routes.MapRoute(
"Markets",
"{controller}/{keyword}/{page}",
new
{
controller = "Markets",
action = "Index",
keyword = UrlParameter.Optional,
page = UrlParameter.Optional
}
);
routes.MapRoute(
"Markets main",
"{controller}/{action}/{id}",
new
{
controller = "Markets",
action = "Index",
id = UrlParameter.Optional,
}
);
...
控制器是:
[HttpGet]
public async Task<ActionResult> Index(string keyword, int page = 1)
{
//search logic and create viewmodel
Return ActionResult(viewmodel)
}
该功能运行良好,如果我在地址栏中键入首选格式并按 Enter,则会显示预期的搜索结果。
如何使表单提交并将值以首选格式放置?