首页 > 解决方案 > ASP.NET MVC - 复选框列表未返回所有逗号分隔值

问题描述

我有以下复选框列表:

<input type="checkbox" name="Categories" value="1" />
<input type="checkbox" name="Categories" value="2" />

我的模型如下:

public class MyModel
{
  public string Categories { get; set; }

}

我的控制器:

 public ActionResult Index(MyModel model)
 {
    if (ModelState.IsValid)
    {

        // Save data to database, and redirect to Success page.

        return RedirectToAction("Success");
    }

 }

选择两个复选框只保存一个值?

标签: asp.net-mvccheckboxlist

解决方案


您不能直接将逗号分隔值获取到服务器,我建议更改类如下

public class MyModel
{
   public List<string> Categories { get; set; }

}

您将获得选中复选框的值列表。

如果您想要逗号分隔值,那么在客户端需要提交表单创建功能并需要保存隐藏变量。

愿这对你有帮助

谢谢


推荐阅读