Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET core asp-for radio button bound to model with clickable labels?

I wanted to make a radio button list which is bound to a model in asp.net core. But, I also wanted the unique labels to be clickable and associated with each radio button. I have this:

  <fieldset>
      <div>
          <span>
              <input type="radio" required asp-for="Email" value="EmailYes" /> <b>Send Email:</b> They will be sent an email.
          </span>
      </div>
      <div>
          <span>
              <input type="radio" required asp-for="Email" value="EmailNo" /> <b>Do Not Send Email:</b> They will not be sent an email.
          </span>
      </div>
  </fieldset>

I've tried adding a label like so:

  <label asp-for="EmailYes"><b>Send Email:</b> They will be sent an email. </label>

But when the user clicks on the second label, it selects the first radio button.

Thanks.

like image 685
Chris J Avatar asked Oct 16 '22 10:10

Chris J


1 Answers

Got it:

<fieldset>
  <div>
      <span>
          <input type="radio" required asp-for="Email" id="EmailYes" value="EmailYes" />
          <label for="EmailYes"><b>Send Email:</b> They will be sent an email. /label> 
      </span>
  </div>
  <div>
      <span>
          <input type="radio" required asp-for="Email" id="EmailNo" value="EmailNo" />
          <label for="EmailYes"><b>Do Not Send Email:</b> They will not be sent an email. /label>
      </span>
  </div>

I was hoping to use only .Net Core tag helpers. But this works.

like image 158
Chris J Avatar answered Oct 20 '22 23:10

Chris J