まず、ここで我慢してください。フォーム データをカスタム オブジェクトに正常にマッピングするカスタム モデル バインダーがあります。このモデル バインダー内では、フォーム項目も別のカスタム オブジェクトにマップされます。私ができるはずだと感じているのは、この 2 番目のマッピングを処理する別のモデル バインダーを作成することです。これは簡易版です。
カスタム オブジェクト:
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
public string Status { get; set; }
public string Description { get; set; }
public IEnumerable<SubCategory> SubCategories { get; set; }
}
public class SubCategory
{
public int CategoryId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Status { get; set; }
}
フォームが SubCategories の一連の Id を返す場合、データ リポジトリに移動して SubCategory オブジェクトをハイドレートする必要があります。フォームから、サブカテゴリのリストが次の形式で送信されます。
<input type="text" name="Name" value="This Category" />
<input type="hidden" name="subcat.Index" value="0" />
<select name="subcat[0].Id">
<option value="1">Something</option>
<option value="2">Something else</option>
</select>
<input type="hidden" name="subcat.Index" value="1" />
<select name="subcat[1].Id">
<option value="1">Something</option>
<option value="2">Something else</option>
</select>
<input type="hidden" name="subcat.Index" value="2" />
<select name="subcat[2].Id">
<option value="1">Something</option>
<option value="2">Something else</option>
</select>
カテゴリをマップするカスタムを作成するのは明らかに簡単ですが、サブカテゴリをマップするモデル バインダーを作成する (モデル バインダー内でデータ リポジトリからクエリを実行する) のは少し難しいことがわかります。
申し訳ありませんが、読んでいただきありがとうございます。これを明確にするために何か言えることがあれば教えてください。