代码之家  ›  专栏  ›  技术社区  ›  adam78

ASP.NET MVC-复选框列表不返回所有逗号分隔的值

  •  0
  • adam78  · 技术社区  · 7 年前

    我有以下复选框列表:

    <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");
        }
    
     }
    

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

    2 回复  |  直到 7 年前
        1
  •  0
  •   Tetsuya Yamamoto    7 年前

    主要问题是 string 属性存储单个字符串,您应该使用集合属性绑定复选框,例如。 List<string> 正如前面提到的@Ajay。

    因此,您应该使用以下设置:

    模型

    public class MyModel
    {
        public MyModel()
        {
            SelectedCategories = new List<string>();
    
            // put categories here
            Categories = new List<SelectListItem>() { ... };
        }
    
        // other properties
    
        public List<string> SelectedCategories { get; set; }
        public List<SelectListItem> Categories { get; set; }
    }
    

    @foreach (var item in Model.Categories)
    {
        <input type="checkbox" name="SelectedCategories" value="@item.Value" ... />
    }
    

    控制器

    [HttpPost]
    public ActionResult Index(MyModel model)
    {
        if (ModelState.IsValid)
        {
            // create comma-separated values
            var selectedCategories = string.Join(",", model.SelectedCategories);
    
            // Save data to database, and redirect to Success page.
    
            return RedirectToAction("Success");
        }
    }
    

    CheckBoxFor 助手,使用 SelectListItem 哪个有 Selected 财产 bool 类型,因为 支票箱 约束 布尔 财产:

    模型

    public class MyModel
    {
        public MyModel()
        {
            // put categories here
            Categories = new List<SelectListItem>() { ... };
        }
    
        // other properties
        public List<SelectListItem> Categories { get; set; }
    }
    

    看法

    @for (var i = 0; i < Model.Categories.Count; i++)
    {
        @Html.CheckBoxFor(model => model.Categories[i].Selected)
        @Html.HiddenFor(model => model.Categories[i].Text)
        @Html.HiddenFor(model => model.Categories[i].Value)
    }
    

    控制器

    [HttpPost]
    public ActionResult Index(MyModel model)
    {
        if (ModelState.IsValid)
        {
            string selectedCategories = string.Join(",", 
                                        model.Categories.Where(x => x.Selected == true)
                                                        .Select(x => x.Text).ToList());
    
            // Save data to database, and redirect to Success page.
    
            return RedirectToAction("Success");
        }
    }
    

    注:

    有一个名为 CheckBoxListFor List<T> 财产。

    可以看到复选框列表实现的示例 here

    相关问题:

    Get comma-separated string from CheckboxList HTML Helper

    Get Multiple Selected checkboxes in MVC

        2
  •  1
  •   Ajay    7 年前

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

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

    若您想要逗号分隔的值,那个么在客户端需要在提交表单时创建函数,并需要保存隐藏变量。

    这对你有帮助吗

    谢谢