表中具有相同名称剃刀的多个隐藏字段

时间:2013-08-17 22:59:05

标签: c# asp.net-mvc-4 razor

我在表格中有多个隐藏的Input字段。每行有三个表单,每个表单都有一个隐藏字段,具有相同的name。但是当我单击其中一个按钮时,只使用最后一个渲染隐藏字段的用户名。为什么会这样,我该如何改变呢?

这是剃须刀标记

<table class="table table-condensed">
    <thead>
        <tr>
            <th>#</th>
            <th>Benutzername</th>
            <th>Aktiviert</th>
            <th>S0Pin Ansicht</th>
            <th>Einstellungen</th>
            <th>Aktionen</th>
        </tr>
    </thead>
    <tbody>
        @if (Model != null && Model.Count > 0) {
            foreach (var item in Model) {
            <tr>
                <td>@item.ID</td>
                <td>@item.Username</td>
                @if (item.Activated) {
                    <td><i id="active_@item.ID" class="icon-ok" /></td>
                } else {
                    <td><i id="active_@item.ID" class="icon-remove" /></td>
                }
                @if (item.S0PinAllowed) {
                    <td><i id="support_@item.ID" class="icon-ok" /></td>
                } else {
                    <td><i id="support_@item.ID" class="icon-remove" /></td>
                }
                @if (item.SettingsAllowed) {
                    <td><i id="settings_@item.ID" class="icon-ok" /></td>
                } else {
                    <td><i id="settings_@item.ID" class="icon-remove" /></td>
                }
                <td>
                    @if (item.Username == User.Identity.Name) {
                        <span>Keine Aktionen verfügbar</span>
                    } else {
                        using (Ajax.BeginForm(item.Activated ? "DeactivateUser" : "ActivateUser", "Settings", new AjaxOptions() {
                            HttpMethod = "POST",
                            UpdateTargetId = "usertable"
                        }, new {
                            id = "formlogin_" + item.ID,
                        })) {
                            var user = item as Token.Creator.Site.Models.User;
                        @Html.Hidden("first", Model.First)
                        @Html.Hidden("count", Model.Fetch)
                        @Html.Hidden("username", user.Username, new { id = "username_login_" + user.ID })
                            if (item.Activated) {
                        <input class="btn btn-danger btn-small pull-left" type="submit" id="sublogin_@item.ID" value="Deaktivieren" />
                            } else { 
                        <input class="btn btn-success btn-small pull-left" type="submit" id="sublogin_@item.ID" value="Aktivieren" />
                            }
                        }
                        using (Ajax.BeginForm(item.S0PinAllowed ? "DeactivateSupportUser" : "ActivateSupportUser", "Settings", new AjaxOptions() {
                            HttpMethod = "POST",
                            UpdateTargetId = "usertable"
                        }, new {
                            id = "formsupport_" + item.ID
                        })) {
                            var user = item as Token.Creator.Site.Models.User;
                        @Html.Hidden("first", Model.First)
                        @Html.Hidden("count", Model.Fetch)
                        @Html.Hidden("username", user.Username, new { id = "username_support_" + user.ID })
                            if (item.S0PinAllowed) {
                        <input class="btn btn-danger btn-small pull-left" type="submit" id="subsupport_@item.ID" value="Support verweigern" />
                            } else { 
                        <input class="btn btn-success btn-small pull-left" type="submit" id="subsupport_@item.ID" value="Support erlauben" />
                            }
                        }
                        using (Ajax.BeginForm(item.SettingsAllowed ? "DeactivateSettingsUser" : "ActivateSettingsUser", "Settings", new AjaxOptions() {
                            HttpMethod = "POST",
                            UpdateTargetId = "usertable"
                        }, new {
                            id = "formsettings_" + item.ID
                        })) {
                            var user = item as Token.Creator.Site.Models.User;
                        @Html.Hidden("first", Model.First)
                        @Html.Hidden("count", Model.Fetch)
                        @Html.Hidden("username", user.Username, new { id = "username_settings_" + user.ID })
                            if (item.SettingsAllowed) {
                        <input class="btn btn-danger btn-small pull-left" type="submit" id="subsettings_@item.ID" value="Einstellungen verweigern" />
                            } else {
                        <input class="btn btn-success btn-small pull-left" type="submit" id="subsettings_@item.ID" value="Einstellungen erlauben" />
                            }
                        }
                    }
                </td>
            </tr>
            }
        } else {
            <tr class="alert alert-info">
                <td colspan="6">Leider sind keine Daten verfügbar :(</td>
            </tr>
        }
    </tbody>
</table>

这是渲染的标记

<table class="table table-condensed">
    <thead>
        <tr>
            <th>#</th>
            <th>Benutzername</th>
            <th>Aktiviert</th>
            <th>S0Pin Ansicht</th>
            <th>Einstellungen</th>
            <th>Aktionen</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>KIRK-NOTEBOOK\Kirk</td>
            <td><i class="icon-ok" id="active_1"></i></td>
            <td><i class="icon-ok" id="support_1"></i></td>
            <td><i class="icon-ok" id="settings_1"></i></td>
            <td>
                <span>Keine Aktionen verfügbar</span>
            </td>
        </tr>
        <tr>
            <td>19</td>
            <td>KIRK-NOTEBOOK\Heather</td>
            <td><i class="icon-ok" id="active_19"></i></td>
            <td><i class="icon-ok" id="support_19"></i></td>
            <td><i class="icon-ok" id="settings_19"></i></td>
            <td>
                <form id="formlogin_19" action="/Site/Settings/DeactivateUser?Length=8" method="post" novalidate="novalidate" data-ajax-update="#usertable" data-ajax-mode="replace" data-ajax-method="POST" data-ajax="true">
                    <input name="first" id="first" type="hidden" value="0" data-val-required='Das Feld "First" ist erforderlich.' data-val-number='Das Feld "First" muss eine Zahl sein.' data-val="true"><input name="count" id="count" type="hidden" value="50" data-val-required='Das Feld "Count" ist erforderlich.' data-val-number='Das Feld "Count" muss eine Zahl sein.' data-val="true"><input name="username" id="username_login_19" type="hidden" value="KIRK-NOTEBOOK\Heather">
                    <input class="btn btn-danger btn-small pull-left" id="sublogin_19" type="submit" value="Deaktivieren">
                </form>
                <form id="formsupport_19" action="/Site/Settings/DeactivateSupportUser?Length=8" method="post" novalidate="novalidate" data-ajax-update="#usertable" data-ajax-mode="replace" data-ajax-method="POST" data-ajax="true">
                    <input name="first" id="Hidden1" type="hidden" value="0" data-val-required='Das Feld "First" ist erforderlich.' data-val-number='Das Feld "First" muss eine Zahl sein.' data-val="true"><input name="count" id="Hidden2" type="hidden" value="50" data-val-required='Das Feld "Count" ist erforderlich.' data-val-number='Das Feld "Count" muss eine Zahl sein.' data-val="true"><input name="username" id="username_support_19" type="hidden" value="KIRK-NOTEBOOK\Heather">
                    <input class="btn btn-danger btn-small pull-left" id="subsupport_19" type="submit" value="Support verweigern">
                </form>
                <form id="formsettings_19" action="/Site/Settings/DeactivateSettingsUser?Length=8" method="post" novalidate="novalidate" data-ajax-update="#usertable" data-ajax-mode="replace" data-ajax-method="POST" data-ajax="true">
                    <input name="first" id="Hidden3" type="hidden" value="0" data-val-required='Das Feld "First" ist erforderlich.' data-val-number='Das Feld "First" muss eine Zahl sein.' data-val="true"><input name="count" id="Hidden4" type="hidden" value="50" data-val-required='Das Feld "Count" ist erforderlich.' data-val-number='Das Feld "Count" muss eine Zahl sein.' data-val="true"><input name="username" id="username_settings_19" type="hidden" value="KIRK-NOTEBOOK\Heather">
                    <input class="btn btn-danger btn-small pull-left" id="subsettings_19" type="submit" value="Einstellungen verweigern">
                </form>
            </td>
        </tr>
        <tr>
            <td>20</td>
            <td>KIRK-NOTEBOOK\Reemt</td>
            <td><i class="icon-ok" id="active_20"></i></td>
            <td><i class="icon-ok" id="support_20"></i></td>
            <td><i class="icon-ok" id="settings_20"></i></td>
            <td>
                <form id="formlogin_20" action="/Site/Settings/DeactivateUser?Length=8" method="post" novalidate="novalidate" data-ajax-update="#usertable" data-ajax-mode="replace" data-ajax-method="POST" data-ajax="true">
                    <input name="first" id="Hidden5" type="hidden" value="0" data-val-required='Das Feld "First" ist erforderlich.' data-val-number='Das Feld "First" muss eine Zahl sein.' data-val="true"><input name="count" id="Hidden6" type="hidden" value="50" data-val-required='Das Feld "Count" ist erforderlich.' data-val-number='Das Feld "Count" muss eine Zahl sein.' data-val="true"><input name="username" id="username_login_20" type="hidden" value="KIRK-NOTEBOOK\Reemt">
                    <input class="btn btn-danger btn-small pull-left" id="sublogin_20" type="submit" value="Deaktivieren">
                </form>
                <form id="formsupport_20" action="/Site/Settings/DeactivateSupportUser?Length=8" method="post" novalidate="novalidate" data-ajax-update="#usertable" data-ajax-mode="replace" data-ajax-method="POST" data-ajax="true">
                    <input name="first" id="Hidden7" type="hidden" value="0" data-val-required='Das Feld "First" ist erforderlich.' data-val-number='Das Feld "First" muss eine Zahl sein.' data-val="true"><input name="count" id="Hidden8" type="hidden" value="50" data-val-required='Das Feld "Count" ist erforderlich.' data-val-number='Das Feld "Count" muss eine Zahl sein.' data-val="true"><input name="username" id="username_support_20" type="hidden" value="KIRK-NOTEBOOK\Reemt">
                    <input class="btn btn-danger btn-small pull-left" id="subsupport_20" type="submit" value="Support verweigern">
                </form>
                <form id="formsettings_20" action="/Site/Settings/DeactivateSettingsUser?Length=8" method="post" novalidate="novalidate" data-ajax-update="#usertable" data-ajax-mode="replace" data-ajax-method="POST" data-ajax="true">
                    <input name="first" id="Hidden9" type="hidden" value="0" data-val-required='Das Feld "First" ist erforderlich.' data-val-number='Das Feld "First" muss eine Zahl sein.' data-val="true"><input name="count" id="Hidden10" type="hidden" value="50" data-val-required='Das Feld "Count" ist erforderlich.' data-val-number='Das Feld "Count" muss eine Zahl sein.' data-val="true"><input name="username" id="username_settings_20" type="hidden" value="KIRK-NOTEBOOK\Reemt">
                    <input class="btn btn-danger btn-small pull-left" id="subsettings_20" type="submit" value="Einstellungen verweigern">
                </form>
            </td>
        </tr>
        <tr>
            <td>21</td>
            <td>Theo</td>
            <td><i class="icon-ok" id="active_21"></i></td>
            <td><i class="icon-ok" id="support_21"></i></td>
            <td><i class="icon-ok" id="settings_21"></i></td>
            <td>
                <form id="formlogin_21" action="/Site/Settings/DeactivateUser?Length=8" method="post" novalidate="novalidate" data-ajax-update="#usertable" data-ajax-mode="replace" data-ajax-method="POST" data-ajax="true">
                    <input name="first" id="Hidden11" type="hidden" value="0" data-val-required='Das Feld "First" ist erforderlich.' data-val-number='Das Feld "First" muss eine Zahl sein.' data-val="true"><input name="count" id="Hidden12" type="hidden" value="50" data-val-required='Das Feld "Count" ist erforderlich.' data-val-number='Das Feld "Count" muss eine Zahl sein.' data-val="true"><input name="username" id="username_login_21" type="hidden" value="Theo">
                    <input class="btn btn-danger btn-small pull-left" id="sublogin_21" type="submit" value="Deaktivieren">
                </form>
                <form id="formsupport_21" action="/Site/Settings/DeactivateSupportUser?Length=8" method="post" novalidate="novalidate" data-ajax-update="#usertable" data-ajax-mode="replace" data-ajax-method="POST" data-ajax="true">
                    <input name="first" id="Hidden13" type="hidden" value="0" data-val-required='Das Feld "First" ist erforderlich.' data-val-number='Das Feld "First" muss eine Zahl sein.' data-val="true"><input name="count" id="Hidden14" type="hidden" value="50" data-val-required='Das Feld "Count" ist erforderlich.' data-val-number='Das Feld "Count" muss eine Zahl sein.' data-val="true"><input name="username" id="username_support_21" type="hidden" value="Theo">
                    <input class="btn btn-danger btn-small pull-left" id="subsupport_21" type="submit" value="Support verweigern">
                </form>
                <form id="formsettings_21" action="/Site/Settings/DeactivateSettingsUser?Length=8" method="post" novalidate="novalidate" data-ajax-update="#usertable" data-ajax-mode="replace" data-ajax-method="POST" data-ajax="true">
                    <input name="first" id="Hidden15" type="hidden" value="0" data-val-required='Das Feld "First" ist erforderlich.' data-val-number='Das Feld "First" muss eine Zahl sein.' data-val="true"><input name="count" id="Hidden16" type="hidden" value="50" data-val-required='Das Feld "Count" ist erforderlich.' data-val-number='Das Feld "Count" muss eine Zahl sein.' data-val="true"><input name="username" id="username_settings_21" type="hidden" value="Theo">
                    <input class="btn btn-danger btn-small pull-left" id="subsettings_21" type="submit" value="Einstellungen verweigern">
                </form>
            </td>
        </tr>
        <tr>
            <td>22</td>
            <td>KIRK-NOTEBOOK\Theo</td>
            <td><i class="icon-ok" id="active_22"></i></td>
            <td><i class="icon-remove" id="support_22"></i></td>
            <td><i class="icon-ok" id="settings_22"></i></td>
            <td>
                <form id="formlogin_22" action="/Site/Settings/DeactivateUser?Length=8" method="post" novalidate="novalidate" data-ajax-update="#usertable" data-ajax-mode="replace" data-ajax-method="POST" data-ajax="true">
                    <input name="first" id="Hidden17" type="hidden" value="0" data-val-required='Das Feld "First" ist erforderlich.' data-val-number='Das Feld "First" muss eine Zahl sein.' data-val="true"><input name="count" id="Hidden18" type="hidden" value="50" data-val-required='Das Feld "Count" ist erforderlich.' data-val-number='Das Feld "Count" muss eine Zahl sein.' data-val="true"><input name="username" id="username_login_22" type="hidden" value="KIRK-NOTEBOOK\Theo">
                    <input class="btn btn-danger btn-small pull-left" id="sublogin_22" type="submit" value="Deaktivieren">
                </form>
                <form id="formsupport_22" action="/Site/Settings/ActivateSupportUser?Length=8" method="post" novalidate="novalidate" data-ajax-update="#usertable" data-ajax-mode="replace" data-ajax-method="POST" data-ajax="true">
                    <input name="first" id="Hidden19" type="hidden" value="0" data-val-required='Das Feld "First" ist erforderlich.' data-val-number='Das Feld "First" muss eine Zahl sein.' data-val="true"><input name="count" id="Hidden20" type="hidden" value="50" data-val-required='Das Feld "Count" ist erforderlich.' data-val-number='Das Feld "Count" muss eine Zahl sein.' data-val="true"><input name="username" id="username_support_22" type="hidden" value="KIRK-NOTEBOOK\Theo">
                    <input class="btn btn-success btn-small pull-left" id="subsupport_22" type="submit" value="Support erlauben">
                </form>
                <form id="formsettings_22" action="/Site/Settings/DeactivateSettingsUser?Length=8" method="post" novalidate="novalidate" data-ajax-update="#usertable" data-ajax-mode="replace" data-ajax-method="POST" data-ajax="true">
                    <input name="first" id="Hidden21" type="hidden" value="0" data-val-required='Das Feld "First" ist erforderlich.' data-val-number='Das Feld "First" muss eine Zahl sein.' data-val="true"><input name="count" id="Hidden22" type="hidden" value="50" data-val-required='Das Feld "Count" ist erforderlich.' data-val-number='Das Feld "Count" muss eine Zahl sein.' data-val="true"><input name="username" id="username_settings_22" type="hidden" value="KIRK-NOTEBOOK\Theo">
                    <input class="btn btn-danger btn-small pull-left" id="subsettings_22" type="submit" value="Einstellungen verweigern">
                </form>
            </td>
        </tr>
    </tbody>
</table>

1 个答案:

答案 0 :(得分:1)

如果有人遇到此问题,只需使用routeValues属性,在隐藏字段中添加要添加的属性作为routeValues中的属性,请参阅此处:

<table class="table table-condensed">
    <thead>
        <tr>
            <th>#</th>
            <th>Benutzername</th>
            <th>Aktiviert</th>
            <th>S0Pin Ansicht</th>
            <th>Einstellungen</th>
            <th>Aktionen</th>
        </tr>
    </thead>
    <tbody>
        @if (Model != null && Model.Count > 0) {
            foreach (var item in Model) {
            <tr>
                <td>@item.ID</td>
                <td>@item.Username</td>
                @if (item.Activated) {
                    <td><i id="active_@item.ID" class="icon-ok" /></td>
                } else {
                    <td><i id="active_@item.ID" class="icon-remove" /></td>
                }
                @if (item.S0PinAllowed) {
                    <td><i id="support_@item.ID" class="icon-ok" /></td>
                } else {
                    <td><i id="support_@item.ID" class="icon-remove" /></td>
                }
                @if (item.SettingsAllowed) {
                    <td><i id="settings_@item.ID" class="icon-ok" /></td>
                } else {
                    <td><i id="settings_@item.ID" class="icon-remove" /></td>
                }
                <td>
                    @if (item.Username == User.Identity.Name) {
                        <span>Keine Aktionen verfügbar</span>
                    } else {
                        using (Ajax.BeginForm(item.Activated ? "DeactivateUser" : "ActivateUser", "Settings", new { username = item.Username }, new AjaxOptions() {
                            HttpMethod = "POST",
                            UpdateTargetId = "usertable"
                        }, new {
                            id = "formlogin_" + item.ID,
                        })) {
                            var user = item as Token.Creator.Site.Models.User;
                        @Html.Hidden("first", Model.First)
                        @Html.Hidden("count", Model.Fetch)
                            if (item.Activated) {
                        <input class="btn btn-danger btn-small pull-left" type="submit" id="sublogin_@item.ID" value="Deaktivieren" />
                            } else { 
                        <input class="btn btn-success btn-small pull-left" type="submit" id="sublogin_@item.ID" value="Aktivieren" />
                            }
                        }
                        using (Ajax.BeginForm(item.S0PinAllowed ? "DeactivateSupportUser" : "ActivateSupportUser", "Settings", new { username = item.Username }, new AjaxOptions() {
                            HttpMethod = "POST",
                            UpdateTargetId = "usertable"
                        }, new {
                            id = "formsupport_" + item.ID
                        })) {
                            var user = item as Token.Creator.Site.Models.User;
                        @Html.Hidden("first", Model.First)
                        @Html.Hidden("count", Model.Fetch)
                            if (item.S0PinAllowed) {
                        <input class="btn btn-danger btn-small pull-left" type="submit" id="subsupport_@item.ID" value="Support verweigern" />
                            } else { 
                        <input class="btn btn-success btn-small pull-left" type="submit" id="subsupport_@item.ID" value="Support erlauben" />
                            }
                        }
                        using (Ajax.BeginForm(item.SettingsAllowed ? "DeactivateSettingsUser" : "ActivateSettingsUser", "Settings", new { username = item.Username }, new AjaxOptions() {
                            HttpMethod = "POST",
                            UpdateTargetId = "usertable"
                        }, new {
                            id = "formsettings_" + item.ID
                        })) {
                            var user = item as Token.Creator.Site.Models.User;
                        @Html.Hidden("first", Model.First)
                        @Html.Hidden("count", Model.Fetch)
                            if (item.SettingsAllowed) {
                        <input class="btn btn-danger btn-small pull-left" type="submit" id="subsettings_@item.ID" value="Einstellungen verweigern" />
                            } else {
                        <input class="btn btn-success btn-small pull-left" type="submit" id="subsettings_@item.ID" value="Einstellungen erlauben" />
                            }
                        }
                    }
                </td>
            </tr>
            }
        } else {
            <tr class="alert alert-info">
                <td colspan="6">Leider sind keine Daten verfügbar :(</td>
            </tr>
        }
    </tbody>
</table>

这很有用。