Reputation: 673
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?
Upvotes: 1
Views: 2860
Reputation: 1
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.
Upvotes: 0
Reputation: 1038710
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.
Upvotes: 2