私のビューモデルには、オブジェクトのリストがあります。これらのオブジェクトを繰り返し、それぞれのコントロールを作成します。以下の状況では、オブジェクトごとにテキストボックスとボタンを表示したいと思います。ユーザーがボタンをクリックすると、投稿が作成され、コントローラーにデータを保存できます。
UIで、ユーザーは必要なフォームを変更し、[保存]をクリックできます。
私の問題は、モデルがコントローラーに投稿されたときにモデルがnullになることです。
私のかみそりコード:
using (Html.BeginForm())
{
foreach (var contributor in Model.Contributor)
{
@Html.HiddenFor(model => contributor.Id)
<div class="formrow">
@Html.ValidationSummary(true)
</div>
<h2>@Html.TextRaw("AuthorInfo", "Author")</h2>
<div class="formrow">
@Html.EditorFor(model => contributor.FirstName)
<div class="formvalidation">
@Html.ValidationMessageFor(model => contributor.FirstName)
</div>
</div>
<div class="formrow right">
<input type="hidden" name="formsubmitted" value="true" />
<input type="submit" class="button" value="@Html.Text("ButtonText", "Save")" />
</div>
}
}
私のビューモデルコード
public class ProfileModel
{
public string Message { get; set; }
public List<PublisherModel> Publisher { get; set; }
public List<ContributorModel> Contributor { get; set; }
public ContributorModel NewContributor { get; set; }
}
私のコントローラーコード
[HttpPost]
public ActionResult Mine(ProfileModel model, string newuser)
{
//
}
それを修正する方法は?
何らかの方法で変更を保存する方法でビューモデルを拡張する必要があると思います。しかし、私は本当にその方法を見ることができません。
現在、ProfileModelのすべてのプロパティは、コントローラーに到達するとnullになります。
何か案は?