gpt4 book ai didi

asp.net-mvc-3 - 将 Html.RadioButtonFor 与 bool 值一起使用不会写入 Checked ="Checked"

转载 作者:行者123 更新时间:2023-12-01 18:58:29 25 4
gpt4 key购买 nike

我在使用 RadioButtonFor 帮助程序时遇到问题。当传入的值为 true 时,它​​不会在任一单选按钮中显示“检查”。当值为 false 时,它​​工作得很好。

我从我正在处理的项目中复制了此代码并创建了一个示例应用程序,并且我能够复制该问题。如果我将值硬编码为 true 或 false,它似乎可以工作,但是当我使用“!string.IsNullOrEmpty(allgroups)”时,它不起作用。

从 View 中:

<div>
@Html.RadioButtonFor(m => m.AllGroups, true) All Groups
@Html.RadioButtonFor(m => m.AllGroups, false) Current Groups
</div>

从 View 模型:

    public bool AllGroups { get; set; }

来自 Controller :

public ActionResult Index(string allgroups)
{
var model = new ProgramGroupIndexViewModel
{
AllGroups = !string.IsNullOrEmpty(allgroups)
};
return View(model);
}

从 IE 中查看源代码:

<div>
<input id="AllGroups" name="AllGroups" type="radio" value="True" /> All Groups
<input id="AllGroups" name="AllGroups" type="radio" value="False" /> Current Groups
</div>

当 AllGroups 的值为 false 时从查看源代码(注意它有效):

<div>
<input id="AllGroups" name="AllGroups" type="radio" value="True" /> All Groups
<input checked="checked" id="AllGroups" name="AllGroups" type="radio" value="False" /> Current Groups
</div>

最佳答案

模型绑定(bind)变得困惑,因为您将操作参数命名为与模型属性相同。更改 Index 操作参数的名称,它应该可以工作。

public ActionResult Index(string showAllGroups)
{
var model = new ProgramGroup
{
AllGroups = !string.IsNullOrEmpty(showAllGroups);
};
return View(model);
}

关于asp.net-mvc-3 - 将 Html.RadioButtonFor 与 bool 值一起使用不会写入 Checked ="Checked",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10629671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com