現在のプロジェクトで何度も使用したパターンに従っていますが、作成した最新のビューでは、BeginForm() は POST ではなくアクション メソッドの GET ルートを解決しています。何が違うのかわからないので、本当に困惑しています。以下のコード例:
public class FooController:Controller {
[HttpGet]
public ActionResult Bar(int id) {
return View(new Model(id));
}
[HttpPost]
public ActionResult Bar(Model model) {
//do stuff with the model
}
}
//Bar View
<script type="text/javascript">
$(document).ready(function() {
$('#formName').submit(function() {
if(/* invalid input */) {
//set html for an error div
return false;
}
});
});
</script>
@model Model
@using(Html.BeginForm("Bar", //action name
"Foo", //controller name
null, //explicit null for route values
FormMethod.Post //explicitly set form method
new {id="formName"})){ //explicitly id the form
//bunch of inputs, a la
@Html.TextBox("Property", Model.Property)
}
//Bar html
<form id="formName" action="/Foo/Bar/{Model Id}" method="post">
<!-- input elements, etc -->
</form>
私が試した BeginForm のすべてのオーバーロードは、アクションに続く Id でルートを解決したため、フォームが GET メソッドに POST するたびに。
POST メソッドの名前を「BarBar」などに変更し、BeginForm への呼び出しを更新しようとしました。その場合、ルートは正しく解決され、フォームは適切なアクションに POST されました。
私は他の何十ものフォームでこのパターンに従いました - 今回私が何を間違えたのか誰か知っていますか?