我试图为我的搜索栏实现一个搜索查询字符串。
按照Microsoft文档:添加搜索,当我收到错误时,它建议在视图中使用
Html.BeginForm("action", "controller", FormMethod.Get)
方法:
此页面无法正常工作
如果问题仍然存在,请联系网站所有者。
HTTP 错误 405
我的搜索查询字符串显示:
本地主机:7185 / Searchbar__RequestVerificationToken = CfDJ8GCygMwCAINPiWbEfcvWp986TX925YtT_ivBMr4CJ0Cj6g6BDdH6xua1gReYA37rr5ljwc_GCuVXkANiQOt6hWJVJpOLr308aqLyyO6ii9fpf6DIbDlkK xYtOHHrN-O5eOxj4ie-TU-C-uBX2CNp0x4&SearchString=a&__RequestVerificationToken=CfDJ8GCygMwCAINPiWbEfcvWp986TX925YtT_ivBMr4CJ0Cj6g6BDdH6xua1gReYA37rr5ljwc_GCuVXkaNiQOt6hWJVJpOL r308aqLyyO6ii9fpf6DIbDlkKxYtOHHrN-O5eOxj4ie-TU-C-uBX2CNp0x4
我在控制器中使用了
[ValidateAntiForgeryToken]
:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Index(string searchString)
{
//Code
}
连同视图中的
@Html.AntiForgeryToken()
:
@using (Html.BeginForm("Index", "Searchbar", FormMethod.Get))
{
@Html.AntiForgeryToken()
<form class="d-flex w-50 ps-lg-5" role="search" asp-controller="Searchbar" asp-action="Index">
<div class="input-group">
<input class="form-control shadow-none" value="@TempData["searchString"]" type="search" placeholder="Search" id="text-suggestion" aria-label="Search" name="SearchString"
required oninvalid="this.setCustomValidity(' ')" />
<button class="btn btn-outline-success shadow-none" type="submit" id="search-button"><i class='bx bx-search'></i></button>
</div>
</form>
}
谢谢你。
[HttpPost]
。
<form method="get">
,因此您不能在呈现为 [HttpPost]
的控制器中使用 <form method="post">
。public async Task<IActionResult> Index(string searchString)
{
//Code
}
<form>
元素
<form>
元素时,现有的 <form>
标记帮助程序将充当 子元素。@using (Html.BeginForm("Index", "Searchbar", FormMethod.Get))
{
<div class="input-group d-flex ps-lg-5">
<input class="form-control shadow-none" value="@TempData["searchString"]" type="search" placeholder="Search" id="text-suggestion" aria-label="Search" name="SearchString"
required oninvalid="this.setCustomValidity(' ')" />
<button class="btn btn-outline-success shadow-none" type="submit" id="search-button"><i class='bx bx-search'></i></button>
</div>
}
谢谢你。