我正在尝试处理asp MVC 5中的单选按钮ids。
我是这样工作的
示例模型
class A
{
public bool? radio { get; set; }
}在剃刀视图中
@Html.RadioButtonFor(x => x.NeutroBT, Model.NeutroBT, new { @id = "True"})
@Html.RadioButtonFor(x => x.NeutroBT, !Model.NeutroBT, new { @id = "False})这不会造成问题,但我在一个编辑器模板中工作,我希望它生成id,以便以某种方式从其他视图访问@Html.IdFor(x => x.NeutroBT, true)和@Html.IdFor(x => x.NeutroBT, false),只是为了防止将来发生更改
像这样的可能吗?我花了很多时间寻找,但没有找到任何类似的东西
如果不可能,最好的处理方法是什么?
谢谢!
发布于 2018-08-27 20:38:26
不需要使用id属性。相反,您可以只使用javascript属性来选择或设置值(在任何情况下,@Html.IdFor() will only ever returnNeutroBT, not theidthat you override in theRadioButtonFor()`方法,因此它不能在您的情况下使用)。
另外,RadioButtonFor()的第二个参数应该是true或false (不是Model.NeutroBT和!Model.NeutroBT)。
要将标签与按钮相关联,可以将其包装在<label>中,例如
<label>
@Html.RadioButtonFor(x => x.NeutroBT, true, new { id = ""})
<span>Yes</span>
</label>
<label>
@Html.RadioButtonFor(x => x.NeutroBT, false, new { id = ""})
<span>No</span>
</label>请注意,new { id = "" }删除了id属性,并防止由于重复的id属性而导致的无效html。
然后使用jQuery访问选定的值
var selectedValue = $('input[name="' + @Html.NameFor(x => x.NeutroBT) + '"]:checked').val();https://stackoverflow.com/questions/52038509
复制相似问题