gpt4 book ai didi

asp.net-core - asp.net core 单选按钮助手为每个单选按钮呈现始终相同的 id

转载 作者:行者123 更新时间:2023-12-01 21:47:50 27 4
gpt4 key购买 nike

创建.cshtml//为简洁起见,删除了其余表单代码

<div class="form-group">
<label for="GradingKey.HalfScoresCount">
<input asp-for="GradingKey.HalfScoresCount" value="true" type="radio" />Yes
</label>
<span asp-validation-for="GradingKey.HalfScoresCount" class="text-danger" />
</div>

<div class="form-group">
<label for="GradingKey.HalfScoresCount">
<input asp-for="GradingKey.HalfScoresCount" value="false" type="radio" />No
</label>
<span asp-validation-for="GradingKey.HalfScoresCount" class="text-danger" />
</div>

已创建 Html

<div class="form-group">
<label for="GradingKey.HalfScoresCount">
<input value="true" type="radio" data-val="true" data-val-required="Das Feld &quot;HalfScoresCount&quot; ist erforderlich." id="GradingKey_HalfScoresCount" name="GradingKey.HalfScoresCount" />Yes
</label>
<span class="text-danger field-validation-valid" data-valmsg-for="GradingKey.HalfScoresCount" data-valmsg-replace="true" />
</div>

<div class="form-group">
<label for="GradingKey.HalfScoresCount">
<input value="false" type="radio" id="GradingKey_HalfScoresCount" name="GradingKey.HalfScoresCount" />No
</label>
<span class="text-danger field-validation-valid" data-valmsg-for="GradingKey.HalfScoresCount" data-valmsg-replace="true" />
</div>

为什么两个单选按钮生成的 html 具有相同的 id?

我怎样才能避免这种情况?

最佳答案

Why has the generated html the same id for both radio buttons?

默认情况下,inputid 字段将是 asp-for 标记帮助器值中设置的属性名称。您已将相同的值列出两次:

<input asp-for="GradingKey.HalfScoresCount" value="true" type="radio" />Yes
<input asp-for="GradingKey.HalfScoresCount" value="false" type="radio" />No

How can I avoid that?

只需指定您自己的 id 属性值即可:

<input asp-for="GradingKey.HalfScoresCount" value="true" type="radio" id="myFirstId" />Yes
<input asp-for="GradingKey.HalfScoresCount" value="false" type="radio" id="mySecondId" />No

只要不修改 name 属性,模型绑定(bind)仍然可以正常工作。

关于asp.net-core - asp.net core 单选按钮助手为每个单选按钮呈现始终相同的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39377957/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com