2012-04-20 2 views
10

を提出し、私はこの記事の指示に続く: Asp.net mvc3 razor with multiple submit buttons を、ここで私のモデルである:MVC4 - 一つの形態2ボタン

public class AdminModel 
{ 
    public string Command { get; set; } 
} 

マイコントローラー

[HttpPost] 
public ActionResult Admin(List<AdminModel> model) 
{ 
    string s = model.Command; 
} 

マイビュー

@using (Html.BeginForm("Admin", "Account")) 
{ 
    <input type="submit" name="Command" value="Deactivate"/> 
    <input type="submit" name="Command" value="Delete"/> 
} 

返信すると、文字列 "s"は常にnullです。

また、私はこのフォーラム投稿の2番目の回答(146票の回答)を試しました:How do you handle multiple submit buttons in ASP.NET MVC Framework?とthatsもnullです。私は間違って何をしていますか?

+0

http://stackoverflow.com/questions/2423041/using -two-submit-buttons-single-form/2426152#2426152 どうやってですか? – takepara

答えて

24

あなたはボタンの名前で自分のサーバ側から値を取る必要があり、

public ActionResult Admin(List<AdminModel> model,string Command) 
{ 
    string s = Command; 
} 
+0

私はこれを試したと思っていました。私はその時間違ったことを知らない。それは今、ありがとうございます。 –

+0

'

0

投稿されたコードからわかるように、モデルのリストをコントローラに送信するのではなく、モデルインスタンスを1つだけ送信します。これにコントローラを変更してみてください。

[HttpPost] 
public ActionResult Admin(AdminModel model) 
{ 
    string s = model.Command; 
} 
+0

私の見解ではAdminModelのリストを取っていました:) –

関連する問題