2017-08-04 63 views
-1

我已經在stackoverflow和google上搜索過,並且發現了很多關於這個主題的文章,但是我仍然無法知道我做錯了什麼!我真的很絕望。例如。 How to handle two submit buttons on MVC view一個表格上的多個按鈕

我只是想在我看來有三個按鈕。我確實在控制器中執行了操作,但是,字符串比較不起作用。出於某種原因,我無法打破我的斷點。

這裏是我的代碼: 查看:

@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

嘗試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

謝謝Ashley。我無法向自己解釋,但它仍然不起作用!這是令人難以置信的。 – Nearshore