In MVC 6, how to code checkbox list in view and pass the checked values to the controller?

后端 未结 2 1577
生来不讨喜
生来不讨喜 2020-12-09 21:12

Sorry but most of my searches take me to old MVC codes. Any help will be appreciated.

In MVC 6 with tag helpers, how do you code a set of checkboxes:

2条回答
  •  一整个雨季
    2020-12-09 21:37

    This is how the syntax should be in your for each asp-for should be wrapped in a string with quotation marks

     @foreach (var option in Model.PhoneOptions)
            {
                
    @{ string cbId = "PhoneOption_" + @option.OptionId; } @Html.Label(@cbId.ToString(), @option.OptionName) @*This is causing invalid operation exception*@ @**@
    }

提交回复
热议问题