我正在使用 ASP.NET MVC 4 和 C# 创建一个站点。主页是一个搜索栏,带有一个带有标签“搜索”的提交按钮。我希望用户能够输入音乐艺术家并按下“搜索”按钮。该站点随后应转到包含结果的“结果”页面。
我在按下搜索按钮后无法访问结果页面。
我的 View Index.cshtml 表单如下所示:
<form method="post" class="form-inline">
<div class="row">
<div class="col-md-1" style="min-width:300px">
<input type="text" name="Artist" class="form-control box-shadow--4dp" placeholder="Artist Name" />
</div>
<div class="col-md-2">
<input type="submit" class="btn btn-default box-shadow--4dp" value="Search" />
</div>
</div>
</form>
我的 HomeController.cs 看起来像这样:
namespace Task2.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
// edit
[HttpPost]
public ActionResult Index(string Artist)
{
return RedirectToAction("Result", "Home");
}
public ActionResult Contact()
{
ViewBag.Message = "For more information you can contact me using the form below.";
return View();
}
public ActionResult Result(string Artist)
{
ViewBag.Message = Artist;
return View();
}
}
}
总而言之,我怎样才能使提交按钮将用户重定向到包含结果的结果页面?
最佳答案
如果您希望您的输入作为查询字符串,那么只需将您的表单方法更改为GET
。它会将您重定向到结果页面,输入为查询字符串。
@using (Html.BeginForm("Result","Home",FormMethod.Get,new {@class= "form-inline" }))
{
<div class="row">
<div class="col-md-1" style="min-width:300px">
<input type="text" name="Artist" class="form-control box-shadow--4dp" placeholder="Artist Name" />
</div>
<div class="col-md-2">
<input type="submit" class="btn btn-default box-shadow--4dp" value="Search" />
</div>
</div>
}
关于c# - 按下提交按钮后如何重定向到下一页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34727755/