问题描述
我想在MVC绑定字典中的值。
I'm trying to bind dictionary values within MVC.
在动作中我有:
model.Params = new Dictionary<string, string>();
model.Params.Add("Value1", "1");
model.Params.Add("Value2", "2");
model.Params.Add("Value3", "3");
和视图我有内:
@foreach (KeyValuePair<string, string> kvp in Model.Params)
{
<tr>
<td>
<input type="hidden" name="Params.Key" value="@kvp.Key" />
@Html.TextBox("Params[" + kvp.Key + "]")
</td>
</tr>
}
但视图不显示的初始值,当表单提交的 PARAMS
属性为null?
推荐答案
您应该看一看这个职位从斯科特Hanselman的:
http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx
you should take a look to this post from scott hanselman:http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx
默认粘结剂才明白格式字典:
The default binder just understand dictionaries in the format:
params[0].key = kvp.key
params[0].value = kvp.value
帕拉姆的索引必须是连续的,从0且没有任何差距开始。目前佣工不支持这一点,所以你应该自己创建表单输入字段。
The index of the param must be sequential, starting from 0 and without any gaps. The current helpers don't support this, so you should create the form input fields by yourself.
您当然可以实现自己的粘结剂,像这样的:
http://siphon9.net/loune/2009/12/a-intuitive-dictionary-model-binder-for-asp-net-mvc/
you can of course implement your own binder, like this one:http://siphon9.net/loune/2009/12/a-intuitive-dictionary-model-binder-for-asp-net-mvc/
这篇关于ASP.NET MVC绑定到字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!