从多个提交按钮调用Controller中的ActionResult时找不到资源

时间:2010-09-29 11:07:06

标签: asp.net-mvc button submit

link text

我正在关注此链接中的答案,我已经完成了这个......

<% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %>
  <input type="submit" name="submitButton" value="Send" />
  <input type="submit" name="submitButton" value="Cancel" />
<% Html.EndForm(); %>

<% Html.BeginForm("Send", "MyController", FormMethod.Post); %>
  <input type="submit" name="button" value="Send" />
<% Html.EndForm(); %>

<% Html.BeginForm("Cancel", "MyController", FormMethod.Post); %>
  <input type="submit" name="button" value="Cancel" />
<% Html.EndForm(); %>

在控制器中使用它......

public class MyController : Controller {
public ActionResult MyAction(string submitButton) {
    switch(submitButton) {
        case "Send":
            // delegate sending to another controller action
            return(Send());
        case "Cancel":
            // call another action to perform the cancellation
            return(Cancel());
        default:
            // If they've submitted the form without a submitButton, 
            // just return the view again.
            return(View());
    }
}

private ActionResult Cancel() {
    // process the cancellation request here.
    return(View("Cancelled"));
}

private ActionResult Send() {
    // perform the actual send operation here.
    return(View("SendConfirmed"));
}

}

但我一直收到资源未找到错误 - 找不到MyController \ MyAction

2 个答案:

答案 0 :(得分:0)

您可能需要确保您拥有MVC可以与您的Controller / Action匹配的路由。类似的东西:

    routes.MapRoute(
        "MyRoute",
        "{controller}/{action}/{submitButton}",
        new { controller = "MyController", action = "MyAction", submitButton = "Default" }
    );

答案 1 :(得分:0)

不要在Form参数中指定“Controller”:

<% Html.BeginForm("MyAction", "My", FormMethod.Post); %>
  <input type="submit" name="submitButton" value="Send" />
  <input type="submit" name="submitButton" value="Cancel" />
<% Html.EndForm(); %>

如果你希望它是MyController,那么链接将是/ My / MyAction,必须将Controller类调用为MyControllerController(不测试)