How do I render a group of checkboxes using MVC 4 and View Models (strongly typed)

后端 未结 3 1108
谎友^
谎友^ 2021-02-01 06:35

I\'m rather new to the ASP.net MVC world and I\'m trying to figure out how to render a group of checkboxes that are strongly typed to a view model. In webforms I would just use

3条回答
  •  花落未央
    2021-02-01 07:14

    For me this works too, and I think this is the simplest (reading the previous answers).

    The viewmodel has a string[] for the check boxes.

        public string[] Set { get; set; }
    

    The view has this code, and you can repeat the input as many times you need. name, id of the input control has to match the name of the property of the viewmodel.

    Set

    On the post method the Set variable is an array, having the checked value(s).

提交回复
热议问题