ASP MVC 从数据库设置 RadioButton

发布于 2024-09-02 07:41:27 字数 387 浏览 2 评论 0原文

今天我有一个对你来说应该很简单的问题。

我的视图中有两个单选按钮:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弥繁 2024-09-09 07:41:27

从助手中删除第三个参数:

<%= Html.RadioButton("Sex", "Male") %> Male 
<%= Html.RadioButton("Sex", "Female") %> Female

并在控制器操作中:

ViewData["Sex"] = "Female";

将检查第二个无线电。

Remove the third parameter from the helper:

<%= Html.RadioButton("Sex", "Male") %> Male 
<%= Html.RadioButton("Sex", "Female") %> Female

And in your controller action:

ViewData["Sex"] = "Female";

Will check the second radio.

紫瑟鸿黎 2024-09-09 07:41:27

不确定您在视图数据中存储的内容,但您可以执行以下操作:

<%=Html.RadioButton("Sex", "Male", ViewData["Sex"] == "Male")%> Male
<%=Html.RadioButton("Sex", "Female", ViewData["Sex"] == "Female")%> Female

如果您的视图数据包含指定的字符串,则在 RadioButton 方法的“Checked”重载中放置一个布尔值。

Not sure about what you are storing in the view data, but you could do something like:

<%=Html.RadioButton("Sex", "Male", ViewData["Sex"] == "Male")%> Male
<%=Html.RadioButton("Sex", "Female", ViewData["Sex"] == "Female")%> Female

Which places a boolean in the 'Checked' overload of the RadioButton method if your view data contains the specified string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文