【问题标题】:Two radio button getting selected两个单选按钮被选中
【发布时间】:2020-06-18 14:00:31
【问题描述】:

我有一组单选按钮(是或否),值从数据库呈现为 boolean ,如果 db 值为 true 则将单选按钮设置为选中 else no。而且现在当我点击是时,是和否都被选中了。

下面是我的代码:

<div class="col-md-3">
                <div class="form-group form-group--float">
                    <div class="radio">
                        <input type="radio" name="sp" id="customRadio1" asp-for="SPLimit" value="1" >
                        <label class="radio__label" for="customRadio1">Yes</label>
                        <input type="radio" name="sp" id="customRadio2" asp-for="SPLimit" value="0">
                        <label class="radio__label"  for="customRadio2">No</label>
                    </div>
                </div>
            </div>

【问题讨论】:

    标签: asp.net-mvc twitter-bootstrap asp.net-core


    【解决方案1】:

    这是我的工作演示:

    型号:

    public class Test
    {
        public int Id { get; set; }
        public bool SPLimit { get; set; }
    }
    

    查看:

    @model IEnumerable<Test>
    @foreach (var item in Model)
    {
        if (item.SPLimit)
        {
            <input type="radio" name="sp" id="customRadio1" asp-for="@item.SPLimit" value="1">
            <label class="radio__label" for="customRadio1">Yes</label>
        }
        else
        {
            <input type="radio" name="sp" id="customRadio2" asp-for="@item.SPLimit" value="0">
            <label class="radio__label" for="customRadio2">No</label>
        }
    }
    

    控制器:

    public class HomeController : Controller
    {
        private readonly MvcProjContext _context;
    
        public HomeController(MvcProjContext context)
        {
            _context = context;
        }
    
    
        public IActionResult Index()
        {
            var model = _context.Test.ToList();
            return View(model);
        }
    }
    

    结果:

    【讨论】:

      【解决方案2】:

      当你得到 db 结果时如何设置值?发送了什么或你用它做什么? 尝试发送日志以找出两者的值。也许这只是显示/设计问题。

      【讨论】:

        猜你喜欢
        • 2023-02-03
        • 1970-01-01
        • 2013-07-27
        • 1970-01-01
        • 2018-04-09
        • 2016-06-09
        • 2013-10-02
        • 2018-10-27
        • 2019-12-21
        相关资源
        最近更新 更多