c# - 按下提交按钮后如何重定向到下一页?

标签 c# asp.net-mvc

我正在使用 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/

相关文章:

javascript - 为什么在 MVC 中从 ajax.post 调用时我的 View 没有渲染

c# - 如何在 WPF 中创建一组单选菜单项?

c# - 何时将某些实体分离到不同的存储库中?

c# - 如何在 WPF 列表框中禁用 Windows 声音?

c# - 如何阻止方法递归调用自己?

javascript - 如果我使用 AJAX 调用它,如何取消 MVC 操作?

c# - 整数和的算法

asp.net-mvc - Azure Active Directory - 存储访问 token 的 MVC 应用程序最佳实践

asp.net - 在 ASP.NET MVC 中使用/Owin 授权静态文件

c# - 导航属性已配置有冲突的多重性 Fluent API