如果我手动创建一个隐藏的属性并使用idfornamefor和手动设置值,它将按预期工作。
// This works and has a value attribute with a value
<input type="hidden" id="@Html.IdFor(x => x.Fields[componentIndex].Value)" name="@Html.NameFor(x => x.Fields[componentIndex].Value)" value="@Model.Fields[componentIndex].Value"/>
// This has a value attribute but it's empty
@Html.HiddenFor(x => x.Fields[componentIndex].Value)
请注意,在这两种情况下,生成的名称和id是相同的。
如果我用一个HiddenFor并提供一个
new { @Value=... }
它也没有设置值。
设置值的HiddenFor(使用相同的代码但使用不同的组件索引)之间的唯一区别是它们是在post上提供的,而这个有问题的是在控制器中设置的。