MVC フォームには 2 つのボタンがあります。
<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />
コントローラーアクションから、どれが押されたかを知るにはどうすればよいですか?
ベストアンサー1
両方の送信ボタンに同じ名前を付ける
<input name="submit" type="submit" id="submit" value="Save" />
<input name="submit" type="submit" id="process" value="Process" />
次に、コントローラーで送信の値を取得します。クリックされたボタンのみがその値を渡します。
public ActionResult Index(string submit)
{
Response.Write(submit);
return View();
}
もちろん、その値を評価して、スイッチ ブロックでさまざまな操作を実行することもできます。
public ActionResult Index(string submit)
{
switch (submit)
{
case "Save":
// Do something
break;
case "Process":
// Do something
break;
default:
throw new Exception();
break;
}
return View();
}