我有这个视图,我需要在其中显示两次相同的项目。所以我传递了我的模型列表,这个列表包含两次相同的项目。
这是我显示它们的方式:
<p>
@using (Html.BeginForm("Confirm", "Inventory"))
{
<table>
<tr>
<th>Item Name</th>
(...)
</tr>
@for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>@Html.DisplayFor(x => x[i].OtrObj.ObjName)</td>
(...)
@Html.HiddenFor(x => x[i].OtrObjID)
</tr>
}
</table>
<div class="float-right">
<input type="submit" value="Ready!"/>
</div>
}
</p>
我确信它发生在视图中,因为就在我发送视图时,列表中的每个项目都带有正确的“ObjID”。
但是当页面被渲染时,结果是这样的:
第 1 项:
<input data-val="true" data-val-number="The field OtrObjID must be a number."
data-val-required="The OtrObjID field is required."
name="[0].OtrObjID" type="hidden" value="1" />
第 2 项:
<input data-val="true" data-val-number="The field OtrObjID must be a number."
data-val-required="The OtrObjID field is required."
name="[1].OtrObjID" type="hidden" value="2" />
为什么?我真的不明白。谁能帮我解决这个问题?
以下是这些项目的 HTML 输出:
<p>
<form action="/Inventory/Confirm" method="post">
<table>
<tr>
<th>Item Name</th>
...
</tr>
<tr>
<td>Item one</td>
<td><input checked="checked" class="state1" name="[0].Destination" style="width: 50px" type="radio" value="State 1" /></td>
<td><input class="state2" name="[0].Destination" style="width: 50px" type="radio" value="State 2" /></td>
<td>
<input class="field1" data-val="true" data-val-number="The field m_Field1 must be a number." data-val-required="The m_Field1 field is required." name="[0].m_Field1" style="width:200px" type="text" value="0,99" />
<span class="field-validation-valid" data-valmsg-for="[0].m_Field1" data-valmsg-replace="false">Field 1 must be a number.</span>
</td>
<td>
<input class="field2" data-val="true" data-val-number="The field m_Field2 must be a number." data-val-required="The m_Field2 field is required." name="[0].m_Field2" style="width:200px" type="text" value="1,00" />
<span class="field-validation-valid" data-valmsg-for="[0].m_Field2" data-valmsg-replace="false">Field 2 must be a number.</span>
</td>
<td>
<input class="field3" data-val="true" data-val-number="The field m_Field3 must be a number." data-val-required="The m_Field3 field is required." name="[0].m_Field3" style="width:200px" type="text" value="1,00" />
<span class="field-validation-valid" data-valmsg-for="[0].m_Field3" data-valmsg-replace="false">Field 3 must be a number.</span>
</td>
<td><a href="/Inventory/DisplayItem/2">See other information</a></td>
<input data-val="true" data-val-number="The field OtrObjID must be a number." data-val-required="The OtrObjID field is required." name="[0].OtrObjID" type="hidden" value="1" />
<input data-val="true" data-val-number="The field ObjID must be a number." data-val-required="The ObjID field is required." name="[0].ObjID" type="hidden" value="1" />
<input name="[0].m_Field1" type="hidden" value="0,99" />
<input name="[0].m_Field2" type="hidden" value="1,00" />
<input name="[0].m_Field3" type="hidden" value="1,00" />
</tr>
<tr>
<td>Item 2</td>
<td><input checked="checked" class="state1" name="[1].Destination" style="width: 50px" type="radio" value="State 1" /></td>
<td><input class="state2" name="[1].Destination" style="width: 50px" type="radio" value="State 2" /></td>
<td>
<input class="field1" data-val="true" data-val-number="The field m_Field1 must be a number." data-val-required="The m_Field1 field is required." name="[1].m_Field1" style="width:200px" type="text" value="0,99" />
<span class="field-validation-valid" data-valmsg-for="[1].m_Field1" data-valmsg-replace="false">Field 1 must be a number.</span>
</td>
<td>
<input class="field2" data-val="true" data-val-number="The field m_Field2 must be a number." data-val-required="The m_Field2 field is required." name="[1].m_Field2" style="width:200px" type="text" value="1,00" />
<span class="field-validation-valid" data-valmsg-for="[1].m_Field2" data-valmsg-replace="false">Field2 must be a number.</span>
</td>
<td>
<input class="field3" data-val="true" data-val-number="The field m_Field3 must be a number." data-val-required="The m_Field3 field is required." name="[1].m_Field3" style="width:200px" type="text" value="1,00" />
<span class="field-validation-valid" data-valmsg-for="[1].m_Field3" data-valmsg-replace="false">Field 3 must be a number.</span>
</td>
<td><a href="/Inventory/DisplayItem/2">See other information</a></td>
<input data-val="true" data-val-number="The field OtrObjID must be a number." data-val-required="The OtrObjID field is required." name="[1].OtrObjID" type="hidden" value="2" />
<input data-val="true" data-val-number="The field ObjID must be a number." data-val-required="The ObjID field is required." name="[1].ObjID" type="hidden" value="2" />
<input name="[1].m_Field1" type="hidden" value="0,99" />
<input name="[1].m_Field2" type="hidden" value="1,00" />
<input name="[1].m_Field3" type="hidden" value="1,00" />
</tr>
</table>
<div class="float-right">
<input type="submit" value="Ready!"/>
</div>
</form>
</p>