简体   繁体   English

在 blazor 中绑定动态值的最佳方法是什么

[英]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.

相关问题 开发需要依赖 dll 的动态 365 插件的最佳方法是什么? - What is the best way to develop a dynamics 365 plugin that requires dependent dlls? 什么是绑定表格数据的最佳方法 - what is Best way to bind the tabular data 问:在 Blazor WASM 应用程序中存储 api 密钥的最佳方式是什么? - Q: What is the best way to store api keys in a Blazor WASM application? 在 Blazor 组件中显示条件内容的最佳方式是什么 - What is the best way to display conditional content in Blazor Components Blazor MailKit 将值绑定到消息 - Blazor MailKit bind values to the message 绑定下拉列表MVC3中的值列表的最佳方法 - Best way to bind list of values in dropdown MVC3 绑定枚举(列表)的最佳方法是什么 <Enum> )到DropDownList? - What's Best way to Bind collection of Enums ( List<Enum> ) to a DropDownList? WPF-将数据从数据库绑定到复选框控件的最佳方法是什么 - WPF - what is the best way to bind data from a database to a checkbox control 每个项目用相同的图标绑定ListView(WPF)的最佳方法是什么 - what is the best way to bind ListView (WPF) with same icon for each item 将键/值对绑定到asp标签的最佳方法是什么 - What's the best way to bind key/value pair to an asp label
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM