.NET core asp-for 单选按钮绑定到具有可点击标签的模型?

问题描述 投票:0回答:1

我想制作一个绑定到asp.net core中的模型的单选按钮列表。但是,我还希望独特的标签可单击并与每个单选按钮相关联。我有这个:

  <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>

我尝试添加这样的标签:

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

但是当用户单击第二个标签时,它会选择第一个单选按钮。

谢谢。

html asp.net .net label
1个回答
5
投票

明白了:

<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="EmailNo"><b>Do Not Send Email:</b> They will not be sent an email. /label>
      </span>
  </div>

我希望仅使用 .NET Core 标记帮助程序,但这可行。

© www.soinside.com 2019 - 2024. All rights reserved.