How to implement Radio Button Prop In EF core Razor pages Code first approach

asked 2022-06-17T12:37:00.243+00:00
Arnab 61 Reputation points
Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
420 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
2,514 questions
No comments
{count} votes

Accepted answer
  1. answered 2022-06-21T06:02:05.403+00:00
    Zhi Lv - MSFT 18,311 Reputation points Microsoft Employee

    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.

    213224-2.gif

    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

    No comments

2 additional answers

Sort by: Most helpful
  1. answered 2022-06-17T13:06:49.07+00:00
    AgaveJoe 16,616 Reputation points

    I recommend the following tutorial to learn radio button basics.

    Working With Radio Buttons in ASP.NET Razor Pages

    Once you understand how radio button works and what build-in feature are available, you should be able to complete this task.


  2. answered 2022-06-21T06:45:54.71+00:00
    Arnab 61 Reputation points

    @Zhi Lv - MSFT off topic, You r the type of guy....for them trust still exits for humanity. Love you a lot.....Just made my day...Love from india my man.

    No comments