Asp.net 将MVC视图绑定到IEnumerable<;KeyValuePair<;字符串,bool>&燃气轮机;问题
我认为:Asp.net 将MVC视图绑定到IEnumerable<;KeyValuePair<;字符串,bool>&燃气轮机;问题,asp.net,asp.net-mvc,razor,model-binding,Asp.net,Asp.net Mvc,Razor,Model Binding,我认为: @{ var i = 0; } @foreach (var field in Model.ConsentQuestions) { <div class="form-group"> <div class="control-label col-md-8"> @Html.Label(string.Format("ConsentQuestions[{0}].Key", i), field.Key
@{
var i = 0;
}
@foreach (var field in Model.ConsentQuestions)
{
<div class="form-group">
<div class="control-label col-md-8">
@Html.Label(string.Format("ConsentQuestions[{0}].Key", i), field.Key)
@Html.Hidden(string.Format("ConsentQuestions[{0}].Key", i), field.Key)
</div>
<p class="col-md-2">
@Html.RadioButton(string.Format("ConsentQuestions[{0}].Value", i), true, htmlAttributes: new { id = string.Format("question{0}-true", i) })
<label for="question@(i)-true">Yes</label>
@Html.RadioButton(string.Format("ConsentQuestions[{0}].Value", i), false, htmlAttributes: new { id = string.Format("question{0}-false", i) })
<label for="question@(i)-false">No</label>
</p>
</div>
i++;
}
@{
var i=0;
}
@foreach(模型中的var字段)
{
@Label(string.Format(“问题[{0}].Key”,i),field.Key)
@Hidden(string.Format(“问题[{0}].Key”,i),field.Key)
@RadioButton(string.Format(“问题[{0}].Value”,i),true,htmlAttributes:new{id=string.Format(“问题{0}-true”,i)})
对
@RadioButton(string.Format(“问题[{0}].Value”,i),false,htmlAttributes:new{id=string.Format(“问题{0}-false”,i)})
不
i++;
}
Model.questions
是一个IEnumerable
(原因是这些问题是用户可定义的)。不管出于什么原因,活页夹都无法理解这一点。通常,这种索引在集合中工作得很好(我正在做一些与其他IEnumerables类似的事情,没有任何问题)。奇怪的是,如果我中断我的验证方法,它会看到问题中有正确数量的项,除了每个KVP是{“”,false}
我想知道如何纠正这个问题,并获得要绑定的表单中的值
编辑:我确实有一个看似可行的解决方案,使用一个从
DefaultModelBinder
继承的类,然后重写GetPropertyValue
以查看controllerContext.HttpContext.Request.Form
。。。虽然这很好,但我仍然想知道为什么在这种情况下它不起作用。正如我在评论中提到的(除了输入错误),您需要使用列表,而不是IEnumerable(因为在运行时绑定器无法对其进行索引),并使用作为索引属性的帮助程序
我的测试模型如下所示:
注意:我将HiddenFor
更改为DisplayFor
以查看问题(请随意调整)
有效的方法是:
后续行动:
我一直在检查生成的输出,唯一的区别是使用第一种绑定样式无法匹配现有的值(第二种可以工作)。这适用于使用实体类或KeyValuePair(没有区别)。看起来,RadioButton
活页夹坏了,但RadioButton for
没有:
@Html.RadioButton("ConsentQuestions[" + i.ToString() + "].Value", true, htmlAttributes: new { id = string.Format("question{0}-true", i) })
@Html.RadioButtonFor(m => m.ConsentQuestions[i].Value, false, htmlAttributes: new { id = string.Format("question{0}-false", i) })
结果是一半工作!:
这显然有点奇怪,因为它的工作原理应该是一样的,但解决方案仍然是只对帮助程序的
版本使用无论如何,强类型代码总是比字符串更可取。只需将您的模型更改为使用字典(这实际上是键值对的列表)。你甚至不需要改变你的观点
比如说:
public ActionResult AnswerQuestions(IEnumerable<KeyValuePair<string, bool>> ConsentQuestions)
{
// Do stuff
}
公共行动结果回答问题(IEnumerable问题)
{
//做事
}
将更改为:
public ActionResult AnswerQuestions(Dictionary<string, bool> ConsentQuestions)
{
// Do stuff
}
公共行动结果回答问题(字典问题)
{
//做事
}
主要区别在于,除了IEnumerable
所具有的属性外,它还具有.Comparer
,.ContainsKey()
,.ContainsValue()
,.Count
,.Keys
,.TryGetValue()
,以及值
以下是关于的完整答案。您是否尝试过使用RadioButtonFor(m=>Promissions[i].Value)
语法传递列表`并使用您的i
索引建立索引?这通常会使索引元素绑定正确。哦,一个附录:这应该是m=>m.i.[i]。在您的示例中是值。在您的测试中,键有什么不同吗?在我的情况下,两者都没有约束力。@emodendroket:对不起,我不明白你的意思。你能澄清一下吗?@Html.DisplayFor(m=>m.problems[i].Key)
。。。在我的例子中,这也是无法绑定的,因此如果页面未能通过验证,它将重新加载所有标签为空。你对此有什么问题吗?@emodendroket:你能发布你的控制器Edit
操作等,这样我就可以更新我的测试项目,看看是否还有其他问题在工作吗?严格来说,Dictionary
实现IEnumerable
public ActionResult AnswerQuestions(IEnumerable<KeyValuePair<string, bool>> ConsentQuestions)
{
// Do stuff
}
public ActionResult AnswerQuestions(Dictionary<string, bool> ConsentQuestions)
{
// Do stuff
}