1

我有这个视图,我需要在其中显示两次相同的项目。所以我传递了我的模型列表,这个列表包含两次相同的项目。

这是我显示它们的方式:

<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>
4

2 回答 2

2

“问题”是你有@model IEnumerable<Model>这会强制[#]前缀。

一种可能的解决方案是将模型包装在 ViewModel 中。

public class ViewModel
{
    public IEnumerable<Model> Models { get; set; }
}

然后使用@model ViewModel

我还想建议使用它,@foreach因为它更清洁。

@foreach (var item in Model.Models) {
    <tr>
        <td>
            @Html.DisplayFor(x => item.ObjName)
            (...)
            @Html.HiddenFor(x => item.OtrObjID)
        </td>
    </tr>
}

注意:我把所有东西都放在里面,因为它不是有效的 HTML,在标签<td>之外<td><th>里面有任何东西。http://www.w3schools.com/tags/tag_tr.asp<tr>

于 2013-03-12T15:35:09.567 回答
1

您可以使用简单的 html 隐藏字段控件

<input type="hidden" value="x[i].OtrObjID" name="OtrObjID" />
于 2013-03-12T16:07:46.333 回答