question

Arnab-7575 avatar image
0 Votes"
Arnab-7575 asked Arnab-7575 answered

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

how-to-implement-radio-button-prop-in-ef-core-razor-pages-code-first-approach


dotnet-entity-framework-coredotnet-aspnet-core-razor
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ZhiLv-MSFT avatar image
1 Vote"
ZhiLv-MSFT answered ZhiLv-MSFT edited

Hi @Arnab-7575,

  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


2.gif (1.7 MiB)
registercshtml.txt (11.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered Arnab-7575 commented

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.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@AgaveJoe I did what they r showing in that tutorial but my problem is I cant Access Genders Through Model (Model.Genders) while writing the foreach Loop.`

       @foreach(var gender in Model.Genders)
                 {
                     <input type="radio" asp-for="Input.Gender" value="@gender" id="Gender@(gender)" />@gender<br />
    
    
                 }

its throwing Error.
`

0 Votes 0 ·
Arnab-7575 avatar image
0 Votes"
Arnab-7575 answered

@ZhiLv-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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.