Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

start a group of radiobuttons unchecked in asp.net mvc

Hólla everybody,

I have a strongly typed view and for one int Rating property, I am generating few radio buttons like so:

@Html.RadioButtonFor(m => m.Rating, 1, new { id = "past_Rating_one"})
@Html.RadioButtonFor(m => m.Rating, 2, new { id = "past_Rating_two"})
@Html.RadioButtonFor(m => m.Rating, 3, new { id = "past_Rating_three"})
@Html.RadioButtonFor(m => m.Rating, 4, new { id = "past_Rating_four"})
@Html.RadioButtonFor(m => m.Rating, 5, new { id = "past_Rating_five"})

of course, I be having labels after each radio button and jquery be fancying it with images and such wonders.

All this is fine, but when the page starts, the radio button with id past_Rating_one be selected already. I be bewildered as to how to start all radio buttons unchecked when page loads.

Sure, can do with jQuery but I want to do it just with the view.

I tried it with this:

@Html.RadioButtonFor(m => m.Rating, 1, new { id = "past_Rating_one", selected="none"})

firebug inspection shows that as a property, but still that darn radio button (first one) be stubbornly checked, so I thought I would seek yer help.

like image 628
LocustHorde Avatar asked Aug 22 '11 16:08

LocustHorde


1 Answers

Try setting Rating as a nullable object in your viewmodel, like:

int? Rating { get; set; }

An integer defaults to zero, so value 0 will be selected when rendering the radiobuttons. I know in your example you don't use the value 0, but I suspect you do in your project. Otherwise I could not think of reason the radiobutton is selected.

In case Rating is an enum, the same thing applies.

like image 126
Robin van der Knaap Avatar answered Nov 03 '22 14:11

Robin van der Knaap