我在 Internet 上四处查看,但似乎没有一个解决方案对我有用。
我有以下视图模型:
public class ConfigsViewModel
{
// iOS Dictionary
public Dictionary<string, object> IosDictionary { get; set; }
// Android Dictionary
public Dictionary<string, object> AndroidDictionary { get; set; }
}
这些字典中的值要么是布尔值,要么是字符串。我想显示一个类似于编辑视图的表格,并且我想绑定这些字典。我的观点是这样的:
@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 操作中,当我检查模型时,而不是字符串作为字典中键的值,该值是一个字符串的数组(文本输入中的值)。在字典中,我将值作为布尔值和字符串,所以我不能使用 TextBoxFor。有没有更好的方法来绑定字典?我应该写一个自定义活页夹吗?如果是这样,它应该是什么样子?
谢谢,
考斯敏