在使用非顺序索引和方法中有多个自定义类型参数时绑定困难



假设我要回复这个表单:

 <form action="/Admin/SaveTestChanges" method="post">
        <input name="index" type="hidden" value="S1"/>
        <input name="[S1].SID" type="hidden" value="1"/>
        <input id="sectionName" name="[S1].SectionName" type="text"  value="Nouns"/>
        <input type="submit" value="Submit" />
    </form>

到此方法:

     public void SaveTestChanges(TestModel Test, List<SectionModel> TestSections,
                List<QuestionModel> TestQuestions, List<ChoiceModel> QuestionChoices)
            {
                // Some implementation
            }

对象SectionModel具有唯一的属性名称,而参数列表中的其他自定义类型都没有,但是在绑定时,它不仅表示TestSections.Count为1,而且对于所有其他没有传递键/值对的集合类型也表示相同。为什么会这样呢?

当使用非顺序索引时,是否可以包括参数名称,而不仅仅是属性名称?即TestSections[S1].SectionName还是[S1].TestSections.SectionName ?

似乎当使用任意索引并将它们存储在隐藏输入中时,您仍然需要在输入名称"Index"前面加上实际参数名称,因为它出现在动作方法中。所以在我的例子中,隐藏的输入name属性应该设置为TestSections.Index,只有"Index"会混淆模型绑定器,结果可能有缺陷。

相关内容

最新更新