大きなフォームのセクションを組み立てるフォームがあります。例えば:
Html.RenderPartial("Partials/MealPreference", Model);
フォームにセクションを動的に追加したいと思います。私の部分的なビューの性質を考えると、モデルも一緒に渡す必要があります。その中で、私は惨めに失敗しています。
私の含まれているページのマークアップ:
<div id="additionalPreference"></div>
<input type="button" value="Add Additional Preference" id="addPreference" />
<script>
$(document).ready(function () {
$('#addPreference').click(function () {
$.ajax({
type: "POST",
url: '@Html("AddPreference", "Main")',
success: function (html) {
$(html).appendTo('#additionalPreference');
console.log(html);
},
error: function (xhr, ajaxOptions, thrownError) {
alert("Error");
},
complete: function () {
console.log("End");
}
});
});
});
</script>
私のコントローラーアクション:
[HttpPost]
public ActionResult AddPreference(FormViewModel model)
{
if (ModelState.IsValid)
{
return PartialView("Partials/AdditionalPreference", model);
}
else
{
return PartialView("Partials/LoadFailed");
}
}
モデルは決して有効ではありません。モデルフォームを含むページをコントローラーアクションに渡すにはどうすればよいですか? これは簡単なことのように思えます (確かに Web フォームの場合) が、私はこれに 3 時間も悩まされていました。