Hi @Arnab ,
public class RegisterModel : PageModel
{
...
[BindProperty]
public InputModel Input { get; set; }
public class InputModel
{
...
[Required]
[BindProperty]
public string Gender { get; set; }
public string[] Genders = new[] { "Male", "Female", };
}
...
}
From the above code in your SO thread, we can see you are using the InputModel to bind the property (Genders/Gender), so, in the OnGetAsync method, we need to create the Input model instance, to prevent the Input model NullReferenceException. Code like this:
public async Task OnGetAsync(string returnUrl = null)
{
ReturnUrl = returnUrl;
Input = new InputModel();
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
}
Then, when displaying the radio button in the view page, the code should be like this: access the Genders via the Input, and set the selected value using the Input.Gender.
@foreach (var gender in Model.Input.Genders)
{
@Html.RadioButtonFor(model => model.Input.Gender, gender) @gender<br />
}
After that, the result is like this: we can get the selected gender via the Input.Gender
property.
You can view the source code from here: 213247-registercshtml.txt 213200-registercshtmlcs.txt
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion