简体   繁体   English

未在加载时选择ASP.NET MVC RadioButtonFor

[英]ASP.NET MVC RadioButtonFor Not Selected On Load

There are many other questions here on Stack Overflow and elsewhere, but I have spent far too much time sifting through them for answers to not finally ask one myself. 在Stack Overflow和其他地方,还有许多其他问题,但是我花了太多时间在这些问题上进行筛选,以寻求最终不要问自己一个问题的答案。 I have tried many of the solutions found here and none of them work. 我尝试了这里找到的许多解决方案,但都无济于事。

My problem is as stated in the title; 我的问题如标题中所述; my radio buttons are not being set on page load and I can't figure out as to why. 我的单选按钮没有在页面加载时设置,我也不知道为什么。

In the code below my model has a boolean value CanSubmit, the canSubmitYes and canSubmitNo id's are generated id's using 在下面的代码中,我的模型具有布尔值CanSubmit,canSubmitYes和canSubmitNo id是使用生成的id

ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId("canSubmitYes")

and

ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId("canSubmitNo")

My radio buttons and corresponding labels are currently set with the following: 我的单选按钮和相应的标签当前设置如下:

@Html.LabelFor(Function(m) m.CanSubmit, "Yes", New With {.for = canSubmitYes})
@Html.RadioButtonFor(Function(m) m.CanSubmit, True, New With {.id = canSubmitYes, .class = "submit-toggle-yes"})

@Html.LabelFor(Function(m) m.CanSubmit, "No", New With {.for = canSubmitNo})
@Html.RadioButtonFor(Function(m) m.CanSubmit, False, New With {.id = canSubmitNo, .class = "submit-toggle-no"})

What I have tried: 我尝试过的

  • Setting the checked="checked" properties in the the helpers HTMLAttributes collection 在助手HTMLAttributes集合中设置checked="checked"属性
  • Strip out all of the HTMLHelper code stuff and simply use basic <input type="radio" /> controls and for some reason it's STILL not being checked properly on load. 剥离所有HTMLHelper代码内容,仅使用基本的<input type="radio" />控件,由于某种原因,它仍然无法在加载时正确检查。
  • Tried using a jQuery event to set the 'checked' property of them on load of the document, this too, did not work. 尝试使用jQuery事件在文档加载时设置它们的“ checked”属性,但这也不起作用。 (via .prop("checked", true) ) (通过.prop("checked", true)
  • Changed my format of the html helper from "false" to false to try to force it to understand that the property is a boolean and not a string 我将html助手的格式从"false"更改为false ,以强制其理解该属性是布尔值而不是字符串

There are no other javascript events setting values to these controls. 这些控件没有其他javascript事件设置值。 When I observe me clicking the buttons there are no HTML values changed on the objects - which leads me to think that the 'clicked'/'checked' property is a pseudo property or state that the browser holds. 当我观察到单击按钮时,对象上的HTML值没有更改-这使我认为'clicked'/'checked'属性是伪属性或浏览器保留的状态。

Have I missed something critical or am I way off base here? 我错过了一些重要的事情吗,或者我离开这里了吗?

Thanks in advance! 提前致谢!

Update 更新资料

I have determined that it has something to do with the local variables I'm using. 我已经确定它与我使用的局部变量有关。 I now have them in basic <input> 's again and if I don't connect them they work as expected. 现在,我再次将它们放在基本的<input> ,如果我不连接它们,它们将按预期工作。 It is only when I try to pair them with a common name (the way to tie them together) that they don't work. 只有当我尝试将它们与通用name配对(将它们绑定在一起的方式)时,它们才起作用。

<input name="radio@(Model.ID)" id="@canSubmitYes" type="radio" class="submit-toggle-yes" @(If(submitterPermission.IsSelected, "checked='checked'", "")) />
<label for="@canSubmitYes">Yes</label>

<input name="radio@(Model.ID)" id="@canSubmitNo" type="radio" class="submit-toggle-no" @(If(submitterPermission.IsSelected, "", "checked='checked'")) />
<label for="@canSubmitNo">No</label>

If I remove the name='' attribute then it correctly checks the proper element on load. 如果我删除name=''属性,那么它将正确检查加载时的正确元素。 I need these items to be unique as there could potentially be 50+ of such parings on the page. 我需要这些项目是唯一的,因为页面上可能有50多个这样的配对。

I just found that I had left this question unanswered. 我只是发现我没有回答这个问题。

We eventually did not find the problem, but ended up following a different approach. 我们最终没有发现问题,但最终采用了另一种方法。 Furthermore I no longer work on that project so I cannot adequately update this question. 此外,我不再从事该项目,因此无法充分更新此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM