ASP MVC 从数据库设置 RadioButton
今天我有一个对你来说应该很简单的问题。
我的视图中有两个单选按钮:
Sex:
<%=Html.RadioButton("Sex", "Male", true)%> Male
<%=Html.RadioButton("Sex", "Female", true)%> Female
我需要根据从数据库返回的值选择一个。我现在尝试的方法是:
ViewData["Sex"] = data.Sex; //Set radio button
但这不起作用。我已经尝试了 isChecked 属性的所有可能组合。我知道 data.Sex 返回“男性”或“女性”。我需要做什么才能检查相应的单选按钮?
I have what should be an easy question for you today.
I have two radio buttons in my view:
Sex:
<%=Html.RadioButton("Sex", "Male", true)%> Male
<%=Html.RadioButton("Sex", "Female", true)%> Female
I need to select one based on the value returned from my database. The way I am trying to do it now is:
ViewData["Sex"] = data.Sex; //Set radio button
But that is not working. I have tried every possible combination of isChecked properties. I know that data.Sex is returning either "Male" or "Female". What do I need to do to check the appropriate radio button?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从助手中删除第三个参数:
并在控制器操作中:
将检查第二个无线电。
Remove the third parameter from the helper:
And in your controller action:
Will check the second radio.
不确定您在视图数据中存储的内容,但您可以执行以下操作:
如果您的视图数据包含指定的字符串,则在 RadioButton 方法的“Checked”重载中放置一个布尔值。
Not sure about what you are storing in the view data, but you could do something like:
Which places a boolean in the 'Checked' overload of the RadioButton method if your view data contains the specified string.