单击按钮时重定向到其他视图

时间:2014-04-11 17:23:52

标签: asp.net-mvc asp.net-mvc-3

我是MVC的新手,我想做的事情似乎很简单,但我无法弄清楚。我有4个按钮,我想要做的是当用户点击其中一个按钮时,你会转到另一个视图。我尝试使用RedirectAction但没有任何反应。

查看

@{
ViewBag.Title = "Index";
}
<h2>
Index</h2>
<p>
<button name="button" value="button1">
    One</button>
<button name="button" value="button2">
    Two</button>
<button name="button" value="button3">
    Three</button>
<button name="button" value="button4">
    Four</button>

CONTROLLER

 public class HomeController : Controller
{
    //
    // GET: /Home/   
    public ActionResult Index(string button)
    {

        if (button == "button1")
        {
            return RedirectToAction("Index", "About");
        }
        else if (button == "button2")
        {
            return RedirectToAction("Index", "Contact");
        }

        return View();
    }
}

1 个答案:

答案 0 :(得分:3)

为了使按钮实际提交到服务器,您需要在按钮声明上type="submit"。您还需要在HTML中使用<form>元素来告知其提交位置。

@using( Html.BeginForm() )
{
    <button name="button" value="button1" type="submit">One</button>
    <button name="button" value="button2" type="submit">Two</button>
    <button name="button" value="button3" type="submit">Three</button>
    <button name="button" value="button4" type="submit">Four</button>
}
相关问题