我不知道为什么,但是当我运行应用程序时,此按钮将显示在表格上方。此按钮应在此表下方。我怎样才能解决这个问题?

 <table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.MyList.FirstOrDefault().Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.MyList.FirstOrDefault().Amount)
        </th>
    </tr>

    @using (Html.BeginForm())
    {
        @Html.ValidationSummary("", new { @class = "text-danger" })
        <br />
        for (var i = 0; i < Model.MyList.Count; i++)
        {
            <tr>
                <td>
                    @Html.DisplayFor(model => model.MyList[i].Name)
                    @Html.HiddenFor(model => model.MyList[i].Name)
                </td>
                <td>
                    @Html.TextBoxFor(model => model.MyList[i].Amount)
                    @Html.HiddenFor(model => model.MyList[i].Amount)
                </td>
            </tr>

        }
        <input type="submit" value="Confirm" class="btn btn-success" />
    }
</table>

最佳答案

尝试这个。您正在将表代码与HTML的其余部分混合在一起

@using (Html.BeginForm())
{
    @Html.ValidationSummary("", new { @class = "text-danger" })
    <br />
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.MyList.FirstOrDefault().Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.MyList.FirstOrDefault().Amount)
            </th>
        </tr>


        @for (var i = 0; i < Model.MyList.Count; i++)
        {
            <tr>
                <td>
                    @Html.DisplayFor(model => model.MyList[i].Name)
                    @Html.HiddenFor(model => model.MyList[i].Name)
                </td>
                <td>
                    @Html.TextBoxFor(model => model.MyList[i].Amount)
                    @Html.HiddenFor(model => model.MyList[i].Amount)
                </td>
            </tr>
        }
    </table>
    <input type="submit" value="Confirm" class="btn btn-success" />
}

09-27 16:01