@ Html.RadioButtonFor By default unchecked

In the ASP.NET MVC4 Razor view, how to ensure that none of the default radio buttons are checked? MVC seems to check it for me, even if I do not declare it to be checked in the following codes.

@Html.RadioButtonFor(model => model.test, true, new { id = "radio_test_True"}) Yes
@Html.RadioButtonFor(model => model.test, false, new { id = "radio_test_False"}) No

thank

+4
source share
1 answer

Set Model.testnull in your controller or model constructor and make sure it is zero. A boolmust be either true or false, nullmeaning that none of them are verified.

public class WhateverTheModelIs
{
    public bool? test { get; set; }

    public WhateverTheModelIs()
    {
        test = null;
    }
}

Note. Setting the value nullis redundant, as it is by default. I wanted to be explicit as an example.

+6

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


All Articles