まず、これはAsp.Net MVC 3アプリケーションの複雑な子オブジェクトのコレクションと非常によく似ていますか?しかし、実際に私にとってこれに完全に答えた答えはありませんでした。
PollItemモデルのリストを保持するPollモデルがあります。私のビューは新しいポーリングを作成するためのものなので、ビューに渡されたモデルは完全に空です。したがって、基本的な投票情報用のテキストボックスがあり、次に、ユーザーが任意の量の投票項目を追加/削除できる下部セクションがあります。どういうわけか、それらのポーリングアイテムのテキストボックスをPollItemのリストにバインドする必要があります。
@using (Html.BeginForm("CreatePoll", "Polls", FormMethod.Post, new { enctype = "multipart/form-data", id = "createPollForm", @class = "cmxform group" }))
{
@Html.TextBoxFor(x => x.Question, new { @class = "title-input full-width prominent", placeholder = "Enter Question Here" })
<ul class="poll-info">
<li>
<p><label>Poll Opens</label></p>
@Html.TextBoxFor(x => x.OpeningDate, new { id = "openingDatePicker" })
</li>
<li>
<p><label>Poll Closes</label></p>
@Html.TextBoxFor(x => x.ClosingDate, new { id = "closingDatePicker" })
</li>
</ul>
<section class="description">
<p><label>Description</label></p>
@Html.TextAreaFor(x => x.Description, new { @class = "full-width", placeholder = "Enter a more detailed description of the poll here." })
</section>
<a class="button"><i class="ico"></i> Attach an object</a>
<section class="module-questions">
<ul>
</ul>
<span class="button link" onclick="addAnswer()"><i class="ico"></i> Add Answer</span>
</section>
}
addAnswer()関数は、リストアイテム内の新しいテキストボックスを、PollItem.Descriptionにバインドされる順序付けされていないリストに追加します。これらは、リストであるPoll.PollItemsに保持されます。説明以外にも、PollItemにはおそらく他の要素がいくつかあります。
動的に追加および削除されるテキストボックスをバインドする方法がわからないため、ここからどこから始めればよいのかさえわかりません。