c# - 如何设置 if checkbox checked 语句?

标签 c# .net asp.net html asp.net-mvc

我有多个动态复选框。我想找出 Controller 中选中了哪个复选框。我怎么做?这就是我的。

HTML

foreach (var items in collection) {
    ...
    <tr><td>
        <input id = "checkbox<%= items.id%>" name ="Checkbox<%= items.id%>" />
    </td></tr>
    ...        
}

Controller

foreach (var item in CheckboxList) {
    string id = item.id;
    if (Collection.Request.Form["Checkbox" + id].ToString()) {
        //do stuff...
    }
}

最佳答案

只有选中的复选框才会返回到表单上。

if(Collection.Request.Form["Checkbox" + id] != null)
{
  // Checked!
}

来自spec (4.01):

When a form is submitted, only "on" checkbox controls can become successful.

关于c# - 如何设置 if checkbox checked 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4807157/

相关文章:

c# - 以编程方式列出 Windows 10 中所有受支持的语言环境

c# - 媒体捕捉 - 手机拍摄的照片较暗

.net - 日期的自然语言解析器(.NET)?

c# - ASP.NET MVC 3 : Is there a way to get a controller's string Name from its Type?

c# - Request.Files 始终为空

c# - 如何在 linq 列表中选择指定的行?

c# - 如何在单个原子操作中移动 C# .NET 中的目录

c# - 您如何在 .Net 项目中使用 'lib' 文件夹来构建依赖项?

c# - 没有页面扩展的 URL 重写规则

asp.net - 如何使 Asp.NET Core MVC 应用程序在授权重定向上使用外部 url?