首页 > 解决方案 > 单选按钮的选中属性未绑定到模型的布尔属性

问题描述

下面是我设置了单选按钮。我将配置作为组名,并且我在该组中有 4 个不同的单选按钮,它们具有不同的 selectionId,并且我将选中属性的第三个选项绑定到布尔选择。当我获取方法时,我可以将选中的属性绑定到数据库中的选择。但是当我选择单选按钮时,我想将选择变为真,这对我来说不会发生模型绑定它以保存方法并将该选择保存在数据库中。列表中的每个模型都将具有相同的 ConfigurationName。

@model List<Model>
    <form method="post" asp-action="Configure" novalidate>

      @foreach (var x in Model)
                {
        @Html.RadioButton(x.Configuration,
                    x.SelectionId,
                    x.Selection,
                    new { id = x.SelectionId })
    }

     <div class="modal-footer">
                    <input type="submit" value="Save" class="btn btn-default" />
                </div>
        </form>

控制器方法

     [HttpPost]
     [ValidateAntiForgeryToken]
public ActionResult Configure(List<Model> modellist)
            {
              //will have have to write save logic here, but first my model does not get value of selection as true when that radio button is clicked
                return RedirectToAction("Index");
            }

标签: asp.net-mvcmodel-view-controller

解决方案


单选按钮将仅回发所选值,该值不是布尔值。

也许您可以在 POST 操作中有一个复杂的模型,您可以将列表作为属性(而不是实际模型),然后使用另一个属性来获取选定的单选按钮,如下所述:https ://stackoverflow.com/a /33464014/218408


推荐阅读