我有這個視圖,其中我需要顯示兩次相同的項目。所以我傳遞了一個我的模型列表,並且這個列表包含兩個相同的項目。在同一個id列表中的2個相同的項目獲取在視圖中修改的id值
我是這樣顯示出來:
<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>
你能後整個HTML輸出,不只是片段? – rae1 2013-03-12 14:45:55
好的,我會爲此做點事情。 – hsim 2013-03-12 14:46:43
@HerveS您是否使用HTML 5? – 2013-03-12 14:48:00