インターネットで調べてみましたが、どの解決策もうまくいかないようです。
私は次のビューモデルを持っています:
public class ConfigsViewModel
{
// iOS Dictionary
public Dictionary<string, object> IosDictionary { get; set; }
// Android Dictionary
public Dictionary<string, object> AndroidDictionary { get; set; }
}
これらの辞書の値は、bool または文字列です。編集ビューのようなテーブルを表示したい、これらの辞書をバインドしたい。私の見解は次のようになります。
@model MobileRebrandingTool.Models.ViewModels.ConfigsViewModel
<div>
@using (Html.BeginForm("SaveAppConfig", "Project", "POST"))
{
<table class="config-table">
<tr>
<th></th>
<th>iOS</th>
<th>Android</th>
</tr>
@foreach (var kvp in Model.IosDictionary)
{
<tr>
<td>
@kvp.Key
</td>
<td>
@Html.EditorFor(model => model.IosDictionary[kvp.Key])
</td>
<td>
@if (Model.AndroidDictionary.ContainsKey(kvp.Key))
{
@Html.EditorFor(model => model.AndroidDictionary[kvp.Key])
}
</td>
</tr>
}
</table>
<input type="submit" value="Save"/>
}
</div>
問題は、SaveAppConfig アクションでモデルをチェックすると、辞書のキーの値として文字列ではなく、値が 1 つの文字列 (テキスト入力の値) の配列になることです。辞書にはブール値と文字列が値として含まれているため、TextBoxFor を使用できません。辞書をバインドするより良い方法はありますか? カスタムバインダーを作成する必要がありますか? もしそうなら、それはどのように見えるべきですか?
ありがとうございました、
コスミン