Resource not found when calling ActionResult from Controller from multiple submit buttons
link text
I am following the answer in this link, I have done this...
<% 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(); %>
With this in the controller...
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"));
}
}
But I keep getting a Resource not found error - Cannot find MyController\MyAction
Don't specify "Controller" in the Form parameter:
<% Html.BeginForm("MyAction", "My", FormMethod.Post); %>
<input type="submit" name="submitButton" value="Send" />
<input type="submit" name="submitButton" value="Cancel" />
<% Html.EndForm(); %>
Link will be /My/MyAction if you want it to be MyController, the Controller class must be called MyControllerController (not tested though)
You may need to ensure you have a route that MVC can match to your Controller/Action. Something like:
routes.MapRoute(
"MyRoute",
"{controller}/{action}/{submitButton}",
new { controller = "MyController", action = "MyAction", submitButton = "Default" }
);
精彩评论