Multiple checkboxes in razor (using foreach)

后端 未结 3 700
失恋的感觉
失恋的感觉 2020-12-06 13:51

I have a problem and I can\'t find solution. I\'m using Razor and it is my VieModel class.

public class GroupToExport
{
    public GroupToExport()
    {
             


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-12-06 13:55

    You are using Incorrect syntax to Map the values back when they are posted, since the checked value of a checkbox is initialised to false by default, that is the reason why it is always false,use sysntax

    @for(int i = 0; i < Model.ExportingGroups.Count(); i++)
                {
                    
                        
                            @Html.CheckBoxFor(modelItem => Model.ExportingGroups[i].ToExport)
                        
                    
                }
     //some divs
    

    This should map back all values you are looking for.

提交回复
热议问题