带有表单字段的大火服务器侧竞赛条件

发布于 2025-02-07 05:36:55 字数 955 浏览 1 评论 0原文

我有一个问题,即单击单击后立即重置单选按钮。

我有以下形式的字段:

”“在此处输入映像说明”

i think think 发生了什么事:

  1. 用户在输入中键入一个值
  2. ,然后单击无线电选项(同时将焦点放在焦点中文本输入)
  3. 此单击“触发文本输入到fire Server sine的OnChange/bind事件”
  4. ,用户在短时间内看到了他们的无线电选项,
  5. 大概是当服务器从输入的OnChange事件中响应时选择的无线电选项,它将重新呈现“无线电”按钮当时它收到的状态 - 即没有无线电选择

代码:

<InputText @bind-Value="_viewModel.Amount"></InputText>
<InputRadioGroup @bind-Value="_viewModel.SelectedOptionId" Name="name">
    @foreach (var option in _viewModel.RadioOptions)
    {
        <InputRadio Name="name" Value="@option.Id.ToString()" />
        <text>&nbsp;</text>@option.Name<br>
    }
</InputRadioGroup>

您如何防止此?

I have an issue where a radio button selection is reset immediately after clicking it.

I have the following fields in a form:

enter image description here

I think what's happening is:

  1. The user types a value into the input
  2. The user then clicks a radio option (whilst having focus in the text input)
  3. This click triggers the onChange/bind event for the text input to fire server side
  4. For a brief moment, the user see's their radio option selected
  5. Presumably when the server responds from the input's onChange event, it re-renders the radio button with the state it received at the time - i.e. no radio selection

Code:

<InputText @bind-Value="_viewModel.Amount"></InputText>
<InputRadioGroup @bind-Value="_viewModel.SelectedOptionId" Name="name">
    @foreach (var option in _viewModel.RadioOptions)
    {
        <InputRadio Name="name" Value="@option.Id.ToString()" />
        <text> </text>@option.Name<br>
    }
</InputRadioGroup>

How do you prevent this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梅倚清风 2025-02-14 05:36:55

这里发生的事情是您绑定到错误的类型。

我认为RadioOption.ID和SelectedOptionID均为int

很好,但是不要将选项的ID转换为其他东西。它将阻碍与所选值的绑定。

   <InputRadio Name="name" Value="@option.Id.ToString()" />  -- wrong
   <InputRadio Name="name" Value="@option.Id" />  -- ok

What is happening here is that you bind to the wrong type.

I assume that both RadioOption.Id and SelectedOptionId are of type int.

That is fine, but then don't convert the Options' Ids to something else. It will hinder binding to the Selected value.

   <InputRadio Name="name" Value="@option.Id.ToString()" />  -- wrong
   <InputRadio Name="name" Value="@option.Id" />  -- ok
心在旅行 2025-02-14 05:36:55

事实证明,inputRadiogroup控制存在问题。

此github问题描述了问题。

基本上,由于始终改变的密钥,它将始终重建其内部元素(因此,在您的情况下,Inputradio组件),导致与上述相同的种族条件。

建议的解决方法(有效)是实施具有以下添加行为的自定义inputradiogroup:

 public class MyInputRadioGroup<TValue> : InputRadioGroup<TValue>
 {
     private string? _name;
     private string? _fieldClass;

     protected override void OnParametersSet()
     {
         var fieldClass = EditContext?.FieldCssClass(FieldIdentifier) ?? string.Empty;
         if (fieldClass != _fieldClass || Name != _name)
         {
             _fieldClass = fieldClass;
             _name = Name;
             base.OnParametersSet();
         }
     }
 }

我相信这将在.net 7中按照此拉请求

It turned out that there is an issue with the InputRadioGroup control.

This GitHub issue describes the problem.

Basically, it will always rebuild its internals (So in your case the InputRadio components) due to an always-changing Key, resulting in the same kind of race condition as above.

The suggested workaround (which worked) is to implement a custom InputRadioGroup with the following added behaviour:

 public class MyInputRadioGroup<TValue> : InputRadioGroup<TValue>
 {
     private string? _name;
     private string? _fieldClass;

     protected override void OnParametersSet()
     {
         var fieldClass = EditContext?.FieldCssClass(FieldIdentifier) ?? string.Empty;
         if (fieldClass != _fieldClass || Name != _name)
         {
             _fieldClass = fieldClass;
             _name = Name;
             base.OnParametersSet();
         }
     }
 }

I believe this will be addressed in .NET 7 as per this pull request

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文