Run a group of radio buttons not checked in asp.net mvc

Greetings to all

I have a strongly typed view and for one int Rating property, I generate several radio buttons like:

 @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 have labels after each switch, and jquery with images and such wonders.

All this is fine, but at the start of the page, the switch with the identifier past_Rating_one already selected. I am confused about how to launch all radio buttons without a mark when loading a page.

Of course, you can do it with jQuery, but I want to do it only with a view.

I tried this:

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

firebug inspection shows that as a property, but still this damn switch (first) will be stubbornly checked, so I thought I would seek help.

+6
source share
1 answer

Try setting Rating as an object with a null value in your view model, for example:

 int? Rating { get; set; } 

The default integer value is zero, so when rendering radio objects, the value 0 will be selected. I know that in your example you are not using the value 0, but I suspect what you are doing in your project. Otherwise, I could not think about the reason that the radio router chose.

In case Rating is an enumeration, the same applies.

+10
source

Source: https://habr.com/ru/post/895611/


All Articles