Ресурс не найден при вызове ActionResult из контроллера из нескольких кнопок отправки - PullRequest
0 голосов
/ 29 сентября 2010

текст ссылки

Я следую за ответом по этой ссылке, я сделал это ...

<% 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 голосов
/ 29 сентября 2010

Не указывайте «Контроллер» в параметре формы:

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

Ссылка будет / My / MyAction, если вы хотите, чтобы она была MyController, класс Controller должен называться MyControllerController (хотя не проверял)

0 голосов
/ 29 сентября 2010

Возможно, вам необходимо убедиться, что у вас есть маршрут, который MVC может соответствовать вашему контроллеру / действию.Что-то вроде:

    routes.MapRoute(
        "MyRoute",
        "{controller}/{action}/{submitButton}",
        new { controller = "MyController", action = "MyAction", submitButton = "Default" }
    );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...