2017-08-04 9 views
-1

私はstackoverflowとgoogleで検索し、件名に関する記事が多数見つかりましたが、私が間違っていることはまだ分かりません!私は本当に必死に感じています。例えば。 How to handle two submit buttons on MVC view1つのフォームに複数のボタンがあります

私は3つのボタンを私の視野に入れようとしています。私はコントローラのアクションをヒットしますが、文字列の比較は機能しません。何らかの理由でブレークポイントを打つことができません。ここで

は私のコードです: ビュー:

@using RequestDB7mvc.Models 
@model MainViewModel 
@{ 
    ViewBag.Title = "Main"; 
} 
<div class="form-group"> 
@using (Html.BeginForm("Search4", "Requests", FormMethod.Post)) 
{ 
    <input type="submit" value="Save" name="command" /> 
    <input type="submit" value="Done" name="command" /> 
    <input type="submit" value="Cancel" name="command" /> 
} 

コントローラー:

[HttpPost] 
public ActionResult Search4(MainViewModel model, string command) 
{ 
    if (command.Equals("Save")) 
    { 
     // Call action here... 
     string f = "break"; 
    } 
    else if (command.Equals("Done")) 
    { 
     // Call another action here... 
     string f = "break"; 
    } 
    else if (command.Equals("Cancel")) 
    { 
     // Call another action here... 
     string f = "break"; 
    } 

    ViewBag.Msg = "Details saved successfully."; 
    return View(); 
} 
+0

はあなたがアクションでコマンドパラメータになっているものへと試してみて、デバッグしました。ブレークポイントを打つことができない場合は、トレースを作成し、 – Shahzad

+0

を試してみてください。コマンドの値(「キャンセル」など)が表示されます。ただし、if文にジャンプしません:-( – Nearshore

+0

try command.Equals( "Cancel"、StringComparison.InvariantCulture) – Shahzad

答えて

0

は、あなたは自分のモデルにcommandを追加する必要があります。

public class MainViewModel 
{ 

    ... 

    public string command { get; set; } 

    ... 
} 

次に、あなたのコントローラで、コマンドパラメータを削除し、モデルからハンドル:

public ActionResult Search4(MainViewModel model) 
{ 
    if (model.command.Equals("Save")) 
    { 
     // Call action here... 
     string f = "break"; 
    } 
    else if (model.command.Equals("Done")) 
    { 
     // Call another action here... 
     string f = "break"; 
    } 
    else if (model.command.Equals("Cancel")) 
    { 
     // Call another action here... 
     string f = "break"; 
    } 

    ViewBag.Msg = "Details saved successfully."; 
    return View(); 
} 
+0

アシュリーありがとうございました。それでも動作しません!信じられません。 – Nearshore

関連する問題