Bind Checkboxes to int array/enumerable in MVC

后端 未结 3 1621
慢半拍i
慢半拍i 2021-02-05 08:46
@Html.CheckBox(\"orderNumbers\", new { value = 1 })
@Html.CheckBox(\"orderNumbers\", new { value = 2 })
@Html.CheckBox(\"orderNumbers\", new { value = 3 })
@Html.CheckBo         


        
3条回答
  •  温柔的废话
    2021-02-05 09:03

    In addition to alok_dida's great answer. Since all the values are integers, you can have your controller code take an array of integers and avoid doing the conversion yourself.

    This works in MVC4+:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(int[] orderNumbers)
    {
        return View();
    }
    

提交回复
热议问题