微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用具有onchange方法的输入字段从对象更新属性以写入数据?

如何解决如何使用具有onchange方法的输入字段从对象更新属性以写入数据?

我正在尝试创建一个“编辑”部分来编辑对象并在数据库中更新它们:

首先,我获得当前对象并根据PropertyType(字符串=文本字段,整数=数字选择器等)加载输入字段:

对象:

enter image description here

放置输入字段:

<EditForm @ref="EditForm" Model="ObjectType" novalidate>
<DataAnnotationsValidator />
@foreach (var property in EditObject.GetType().GetProperties())
{
     @if (property.PropertyType.Name.Equals("String"))
     {
           <SfTextBox Placeholder="@property.Name"
                      Type="InputType.Text"
                      @onchange="@((Microsoft.AspNetCore.Components.ChangeEventArgs __e) => property.SetValue(EditObject,__e.Value))"
                      Value="@property.GetValue(EditObject,null)"
                      CssClass="create-model-item">
           </SfTextBox>

     }
     else if (property.PropertyType.Name.Contains("Int"))
     {
             <SfNumericTextBox TValue="int?" Placeholder="Choose a Number Value="@property.GetValue(EditObject,null)" CssClass="create-model-item">
                               <NumericTextBoxEvents TValue="int?" 
                                       ValueChange="@((Syncfusion.Blazor.Inputs.ChangeEventArgs<int?> __e) => property.SetValue(EditObject,__e.Value))">
                               </NumericTextBoxEvents>
             </SfNumericTextBox>
     }
}
<SfButton OnClick="SendForm" IsPrimary="true" CssClass="mb-3">Create</SfButton>
</EditForm>

现在,我想将“ EditObject”中属性的数据更改为用户已填写的新值,这是通过“ OnChange”方法完成的:

@onchange="@((Microsoft.AspNetCore.Components.ChangeEventArgs __e) => property.SetValue(EditObject,__e.Value))"

但是这给了我这个错误

enter image description here

有人知道出了什么问题吗?

提前谢谢!:)

解决方法

您不必指定事件的类型。

您的@onchange代码需要更改为@onchange="@((__e) => { property.SetValue(EditObject,__e.Value); })"

那应该停止错误。

,

有对文本框组件的双向绑定支持。因此,不需要其他方法即可将新值更新为Model属性。如果您使用双向绑定,则默认情况下将对其进行更新。因此,请尝试使用@ bind-Value属性。

还可以查看下面的UG链接。

UG链接:https://blazor.syncfusion.com/documentation/textbox/data-binding/#two-way-data-binding

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。