3

如何在 cshtml 中调用不同的回发?

@{
    Page.Title = "";
    Layout = "~/_Layout.cshtml";

    if(IsPost)
    {
        // if btn1 is pressed do this

        // if btn2 is pressed do this
    }
}

<div>
    <form action="" method="post">
        <input type="submit" value="btn1" />
    </form>

    <form action="" method="post">
        <input type="submit" value="btn2" />
    </form>
</div>

有没有办法判断哪个被按下?

4

1 回答 1

7

您需要为按钮提供一个“名称”属性,以便它们注册为提交表单的一部分,并且您可以使用该值确定提交的按钮。

@{
    if ( IsPost ) {
       switch ( Request.Form[ "btn" ] ) {
          case "btn1":
              <h2>It was btn1</h2>
              break;
        case "btn2":
              <h2>It was btn2</h2>
              break;
      }
   }
}
<div>
    <form method="post">
        <input type="submit" name="btn" value="btn1" />

        <input type="submit" name="btn" value="btn2" />
    </form>
</div>
于 2013-02-27T19:28:53.407 回答