我试图将元素的动态数组绑定到视图模型,其中可能缺少html中的索引在asp.net中绑定缺失元素的数组mvc
例如,与视图模型
class FooViewModel
{
public List<BarViewModel> Bars { get; set; }
}
class BarViewModel
{
public string Something { get; set; }
}
和此刻的HTML
<input type="text" name="Bars[1].Something" value="a" />
<input type="text" name="Bars[3].Something" value="b" />
<input type="text" name="Bars[6].Something" value="c" />
,酒吧也只是空。我怎么能让模型绑定器忽略任何缺失的元素?即上述将绑定到:
FooViewModel
{
Bars
{
BarViewModel { Something = "a" },
BarViewModel { Something = "b" },
BarViewModel { Something = "c" }
}
}
非常接近,但接受的答案在这个网址有一个更完整的解决方案:http://stackoverflow.com/questions/8598214/mvc3-non-sequential-indices-and-defaultmodelbinder – Levitikon 2012-03-28 03:32:51
@Levitikon - 在您的链接接受的解决方案是过度杀伤。你不需要为每个项目指定一个'.Index'。我已经多次使用上述方法,而不需要链接中描述的额外隐藏输入。另外,Phil Haack是ASP.NET MVC开发团队的成员,所以我非常确定他在他的博客中写的是要走的路。 – amurra 2012-03-28 14:16:00
@amurra在Haack的文章中说他为每个领域使用一个单独的隐藏输入,所以看起来这是必要的。 – Mykroft 2015-05-01 15:00:47