Asp.net mvc MVC5:模型中的字典绑定到视图中的一系列复选框?

Asp.net mvc MVC5:模型中的字典绑定到视图中的一系列复选框?,asp.net-mvc,dictionary,model-binding,Asp.net Mvc,Dictionary,Model Binding,当我将模型发布到控制器以保存它时,控制器会得到一个空字典 哪里出了问题?绑定到工作中有什么特别的事情要做吗 我的模型具有以下属性: 公共词典test{get;set;} 我的控制器在调用视图之前填充一些数据: mymodel.DictionaryTest=newdictionary{{0,false},{1,true},{2,false} “我的视图”使用以下代码正确显示它: @Html.CheckBoxFor(m_ => m_.DictionaryTest[0], new { @clas

当我将模型发布到控制器以保存它时,控制器会得到一个空字典

哪里出了问题?绑定到工作中有什么特别的事情要做吗

我的模型具有以下属性:

公共词典test{get;set;}

我的控制器在调用视图之前填充一些数据:

mymodel.DictionaryTest=newdictionary{{0,false},{1,true},{2,false}

“我的视图”使用以下代码正确显示它:

@Html.CheckBoxFor(m_ => m_.DictionaryTest[0], new { @class = "form-control" })
@Html.CheckBoxFor(m_ => m_.DictionaryTest[1], new { @class = "form-control" })
@Html.CheckBoxFor(m_ => m_.DictionaryTest[2], new { @class = "form-control" })

非常感谢

为了绑定字典,您需要有如下post值:
DictionaryProperty[N]。Key
DictionaryProperty[N]。Value
。因此,您的Razor代码需要如下所示:

@Html.HiddenFor(m_ => m_.DictionaryTest[0].Key)
@Html.CheckBoxFor(m_ => m_.DictionaryTest[0].Value, new { @class = "form-control" })

@Html.HiddenFor(m_ => m_.DictionaryTest[1].Key)
@Html.CheckBoxFor(m_ => m_.DictionaryTest[1].Value, new { @class = "form-control" })

@Html.HiddenFor(m_ => m_.DictionaryTest[2].Key)
@Html.CheckBoxFor(m_ => m_.DictionaryTest[2].Value, new { @class = "form-control" })

然而,如果您的键是整数,那么字典就太过分了。只需使用一个简单的列表。然后,Razor代码将按原样工作。

为了绑定字典,您需要具有如下post值:
DictionaryProperty[N]。Key
DictionaryProperty[N]。Value
。因此,您的Razor代码需要如下所示:

@Html.HiddenFor(m_ => m_.DictionaryTest[0].Key)
@Html.CheckBoxFor(m_ => m_.DictionaryTest[0].Value, new { @class = "form-control" })

@Html.HiddenFor(m_ => m_.DictionaryTest[1].Key)
@Html.CheckBoxFor(m_ => m_.DictionaryTest[1].Value, new { @class = "form-control" })

@Html.HiddenFor(m_ => m_.DictionaryTest[2].Key)
@Html.CheckBoxFor(m_ => m_.DictionaryTest[2].Value, new { @class = "form-control" })

然而,如果您的键是整数,那么字典就太过分了。只需使用一个简单的列表。然后,您的Razor代码将按原样工作。

我想我应该在这里写点东西,因为我尝试了公认的答案,结果出现了编译问题-我不知道你们的情况,但行
字典测试[index]。Key
对我不起作用-从什么时候开始数组访问需要
Key
属性

对于任何想找到答案的人来说,这里有一些小技巧可以让这一切像魔术一样运作。简而言之,这一切都取决于:

为了将多个属性绑定到一个对象中(可以是任何复杂类型的列表或字典),模型绑定器需要知道哪些属性属于组合在一起,否则它会变得混乱

假设您有这样一种类型:

public class TestClass
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}
您希望它绑定到如下方法中:

public ActionResult MyAction(List<TestClass> test)
{
    ...
}
<input name="Prop1" value="FirstP1" />
<input name="Prop2" value="FirstP2" />

<input name="Prop1" value="SecondP1" />
<input name="Prop2" value="SecondP2" />
但是,如果您提交的索引顺序不正确,同样的问题/限制也适用。这里有一个特定的场景是复选框-如果您有一个
字典
(或任何其他键类型)要绑定为复选框,您会记得HTML不会在表单提交中提交未选中的复选框。因此,如果你只勾选了几个项目,你会得到一个部分列表,其中的索引肯定是无序的

解决方案1:在表单提交之前,在客户端执行JS操作来重写索引,以强制它们按顺序进行。完全可行,但很烦人

解决方案2:通过帮助模型绑定器了解要查找的键,使用无序键。非常简单:您只需要一个额外的输入来定义索引:

<input type="hidden" name="test.Index" value="IndexA" />
<input name="test[IndexA].Key" value="IndexAKey" />
<input name="test[IndexA].Value" value="IndexAValue" />

<input type="hidden" name="test.Index" value="SomeOtherIndex" />
<input name="test[SomeOtherIndex].Key" value="SomeOtherIndexKey" />
<input name="test[SomeOtherIndex].Value" value="SomeOtherIndexValue" />

名为
.Index
的额外输入只为模型绑定器提供了排序所需的微调。对于字典,使用dictionary键作为自定义索引非常方便,这样所有内容都可以对齐-感觉有点多余,但它确实可以工作:

<input type="hidden" name="test.Index" value="IndexAKey" />
<input name="test[IndexAKey].Key" value="IndexAKey" />       //<-- totally feel like this line doesn't need to be there, but it works
<input name="test[IndexAKey].Value" value="IndexAValue" />


// 我想我应该在这里写点东西,因为我尝试了公认的答案,结果出现了编译问题——我不知道你们的情况,但行
DictionaryTest[index].Key
对我不起作用——从什么时候开始数组访问需要
Key
属性

对于任何想找到答案的人来说,这里有一些小技巧可以让这一切像魔术一样运作。简而言之,这一切都取决于:

为了将多个属性绑定到一个对象中(可以是任何复杂类型的列表或字典),模型绑定器需要知道哪些属性属于组合在一起,否则它会变得混乱

假设您有这样一种类型:

public class TestClass
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}
您希望它绑定到如下方法中:

public ActionResult MyAction(List<TestClass> test)
{
    ...
}
<input name="Prop1" value="FirstP1" />
<input name="Prop2" value="FirstP2" />

<input name="Prop1" value="SecondP1" />
<input name="Prop2" value="SecondP2" />
但是,如果您提交的索引顺序不正确,同样的问题/限制也适用。这里有一个特定的场景是复选框-如果您有一个
字典
(或任何其他键类型)要绑定为复选框,您会记得HTML不会在表单提交中提交未选中的复选框。因此,如果你只勾选了几个项目,你会得到一个部分列表,其中的索引肯定是无序的

解决方案1:在表单提交之前,在客户端执行JS操作来重写索引,以强制它们按顺序进行。完全可行,但很烦人

解决方案2:通过帮助模型绑定器了解要查找的键,使用无序键。非常简单:您只需要一个额外的输入来定义索引:

<input type="hidden" name="test.Index" value="IndexA" />
<input name="test[IndexA].Key" value="IndexAKey" />
<input name="test[IndexA].Value" value="IndexAValue" />

<input type="hidden" name="test.Index" value="SomeOtherIndex" />
<input name="test[SomeOtherIndex].Key" value="SomeOtherIndexKey" />
<input name="test[SomeOtherIndex].Value" value="SomeOtherIndexValue" />

名为
.Index
的额外输入只为模型绑定器提供了排序所需的微调。对于字典,使用dictionary键作为自定义索引非常方便,这样所有内容都可以对齐-感觉有点多余,但它确实可以工作:

<input type="hidden" name="test.Index" value="IndexAKey" />
<input name="test[IndexAKey].Key" value="IndexAKey" />       //<-- totally feel like this line doesn't need to be there, but it works
<input name="test[IndexAKey].Value" value="IndexAValue" />


//参考。参考。在这种情况下,一个简单的列表很好。非常感谢。在这种情况下,一个简单的列表非常好。谢谢。