asp.net - 我如何确定哪个复选框与每一行(用户)相关

发布于 2024-07-29 13:31:48 字数 1102 浏览 7 评论 0原文

我正在使用 asp.net mvc。 我生成了一个视图,用于检索 asp.net 成员资格表中所有未经批准的用户。 我在它们旁边放置了复选框,以便有人提出视图。 目标是某人应该能够选中某些复选框,单击“保存”,然后返回到 ASP.NET 成员身份,并将这些用户的 IsApprove 标志更改为 true。

当我在控制器类中时,如何提取哪些字段设置为 true?

这是视图代码:

<% using (Html.BeginForm()) {%>
<table id="hor-zebra" border = 2>
<tr><td>User</td><td>Approve</td>
</tr>
<%
    MembershipUserCollection membership = (MembershipUserCollection)ViewData["UnapprovedUsers"];
    foreach (MembershipUser member in membership)
     {
        %><tr><td>
        <%=Html.Encode(member.UserName) %> </td><td>
        <%= Html.CheckBox("Approve:" + member.UserName, false) %>
        </td></tr>
  <%
     }
%>
</table>
<input type="submit" value="Save" /><% } %>

这是控制器代码:

    [AcceptVerbs(HttpVerbs.Post)]
    public void ApproveUsers(FormCollection formCollection)
    {
        Console.Write("I have not idea how i can determine which checkboxes are checked");
    }

I am using asp.net mvc. I have generated a view that retrieve all unapproved users in the asp.net membership table. I have put checkboxes next to them for someone to bring up a view. The goal is that someone should be able to check certain checkboxes, hit save and that will go back to asp.net membership and change the IsApprove flag to true for these users.

How do i extract what fields are set as true when i am in the controller class?

here is the view code:

<% using (Html.BeginForm()) {%>
<table id="hor-zebra" border = 2>
<tr><td>User</td><td>Approve</td>
</tr>
<%
    MembershipUserCollection membership = (MembershipUserCollection)ViewData["UnapprovedUsers"];
    foreach (MembershipUser member in membership)
     {
        %><tr><td>
        <%=Html.Encode(member.UserName) %> </td><td>
        <%= Html.CheckBox("Approve:" + member.UserName, false) %>
        </td></tr>
  <%
     }
%>
</table>
<input type="submit" value="Save" /><% } %>

Here is the controller code:

    [AcceptVerbs(HttpVerbs.Post)]
    public void ApproveUsers(FormCollection formCollection)
    {
        Console.Write("I have not idea how i can determine which checkboxes are checked");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2024-08-05 13:31:48

首先,要小心 Html.Checkbox()。 它不仅呈现一个复选框,还呈现一个隐藏字段。 每个用户(例如本例中的“username1”)将呈现以下内容:

<input id="Approve: username1" name="Approve: username1" type="checkbox" value="true"/>
<input name="Approve: username1" type="hidden" value="false" />

问题和选定的答案 更详细地讨论它。 一种选择是自己为此编写 html,例如:

<input type="checkbox" name="<%=member.UserName%>" />

当将其发布到您的控制器时,您可以通过 Request.Form[member.Username] 检索它。 如果为空,则未选中该框。 如果它的值为“On”,则表示是。

First of all, be careful with Html.Checkbox(). It does not just render a checkbox, but a hidden field as well. Each user (such as "username1" in this case) will have the following rendered:

<input id="Approve: username1" name="Approve: username1" type="checkbox" value="true"/>
<input name="Approve: username1" type="hidden" value="false" />

This question and selected answer discus it in more detail. One option is to just write the html yourself for this, such as:

<input type="checkbox" name="<%=member.UserName%>" />

When this gets posted to your Controller you can retrieve it by Request.Form[member.Username]. If it's null, the box wasn't checked. If it has a value of "On", it was.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文