asp.net-mvc - 单选按钮的选中属性未绑定到模型的布尔属性
问题描述
下面是我设置了单选按钮。我将配置作为组名,并且我在该组中有 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");
}
解决方案
单选按钮将仅回发所选值,该值不是布尔值。
也许您可以在 POST 操作中有一个复杂的模型,您可以将列表作为属性(而不是实际模型),然后使用另一个属性来获取选定的单选按钮,如下所述:https ://stackoverflow.com/a /33464014/218408
推荐阅读
- android - android 模块化 - 通过 Intent 启动时从另一个模块获取回调
- perl - Perl SOAP::LITE — 如何添加 http 请求头
- flutter - 这句话是什么意思:“尝试使用 Stream 替换 ChangeNotifier 超出了该类的范围。”
- php - Symfony 4:定义默认的独白记录器/邮件程序将日志发送到错误的处理程序
- c# - Type.IsPrimitive 适用于泛型类型?
- vue.js - NuxtJS 访问 localhost:3000 附加路由基本 url
- android - Android Studio 未在 AMD Ryzen 上启动 Android 模拟器
- python - 如果类的权重不同,如何绘制逻辑回归模型的 roc 曲线
- twitter-bootstrap - 引导固定顶级课程有问题
- vimeo - 如何在开发过程中测试 Vimeo API