[英]What is the best way to bind dynamics values in blazor
I am using a Dictionary<string,string> to store some options, the key is the name of the option and as you guess the value is the value of the correspendent option.我正在使用 Dictionary<string,string> 来存储一些选项,键是选项的名称,并且您猜该值是对应选项的值。
in my razor file i have this bit of code:在我的 razor 文件中,我有这段代码:
@foreach (KeyValuePair<string, string> option in templates.templatesList[SelectionValue])
{
<tr>
<td>@option.Key</td>
<td><input type="text"/></td>
</tr>
}
And what i'm searching to do is to store the value of the text input into the right value in my Dictionary, sadly we can not use @bind = @option.Value.我正在寻找的是将文本输入的值存储到我的字典中的正确值中,遗憾的是我们不能使用@bind = @option.Value。
My solution for the next googlers:我对下一个谷歌员工的解决方案:
An alternative way to bind a value is to use @onchange option like described here: https://docs.microsoft.com/en-us/aspnet/core/blazor/components/data-binding?view=aspnetcore-3.1绑定值的另一种方法是使用@onchange 选项,如下所述: https://docs.microsoft.com/en-us/aspnet/core/blazor/components/data-binding?view=aspnetcore-3.1
So, i changed the code as follow:所以,我改变了代码如下:
in the razor file:在 razor 文件中:
@foreach (KeyValuePair<string, string> option in templates.templatesList[SelectionValue])
{
<tr>
<td><label>@option.Key</label></td>
<td><input type="text" @onchange="@((ChangeEventArgs __e) => changeOption(__e,option.Key))" /></td>
</tr>
}
public void changeOption(ChangeEventArgs __e, string key)
{
templates.changeValue(SelectionValue, key, __e.Value.ToString());
}
And in My object class (Template)在我的 object class 中(模板)
public void changeValue(string template, string option, string value)
{
this.templatesList[template][option] = value;
}
I don't know if it's the cleanest thing to do it this way or not, but it does the job perfectly.我不知道这样做是否最干净,但它完美地完成了这项工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.