У меня есть две кнопки в приложении MVC3.
<input type="submit" name="command" value="Transactions" />
<input type="submit" name="command" value="All Transactions" />
Когда я нажимаю на кнопку, она отправляет обратно правильно, но у FormCollection нет «командных» клавиш.Я также добавил свойство "команда" в модель, и его значение равно нулю при публикации формы.
public ActionResult Index(FormCollection formCollection, SearchReportsModel searchReportsModel). {
if (searchReportsModel.command == "All Transactions")
...
else
....
}
Я использую IE8.Как я могу использовать несколько кнопок в MVC3?Есть ли решение этой проблемы?Я провел много исследований и не смог найти решение.
Обновление:
Дэйв: Я попробовал ваше решение и выдает ошибку Http 404 "Ресурс не найден".
Вот мой код:
[HttpPost]
[AcceptSubmitType(Name = "Command", Type = "Transactions")]
public ActionResult Index(SearchReportsModel searchReportsModel)
{
return RedirectToAction("Transactions", "Reports", new { ...});
}
[HttpPost]
[ActionName("Index")]
[AcceptSubmitType(Name = "Command", Type = "All Transactions")]
public ActionResult Index_All(SearchReportsModel searchReportsModel)
{
return RedirectToAction("AllTransactions", "Reports", new { ... });
}
public class AcceptSubmitTypeAttribute : ActionMethodSelectorAttribute
{
public string Name { get; set; }
public string Type { get; set; }
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
return controllerContext.RequestContext.HttpContext
.Request.Form[this.Name] == this.Type;
}
}
Проблема была решена после комментирования следующего атрибута удаленной проверки в ViewModel (SearchReportsModel).Похоже, что это ошибка в MVC3:
//[Remote("CheckStudentNumber", "SearchReports", ErrorMessage = "No records exist for this Student Number")]
public int? StudentNumber { get; set; }