MudAutocomplete - 需要在编辑表单上绑定值
Posted
技术标签:
【中文标题】MudAutocomplete - 需要在编辑表单上绑定值【英文标题】:MudAutocomplete - need Bind value on edit form 【发布时间】:2021-12-01 07:44:12 【问题描述】:我在表单上使用 MudAutocomplete 控件。现在,当用户单击网格上的编辑图标时,我将显示一个弹出屏幕,其中包含一个预先填写所选记录数据的表单。现在,所有其他控件都预先填充了绑定值,但对于 MudAutocomplete 控件,它不会发生。请在这里帮助我如何实现此功能。
【问题讨论】:
【参考方案1】:这是一个简单的 sn-p,展示了如何将值加载到自动完成中。你可以在https://try.mudblazor.com/snippet/cOcFbFbUhRRLfdsj玩sn-p
<MudGrid>
<MudItem xs="12" sm="6">
<MudButton OnClick="LoadFormData">
Load Form Data
</MudButton>
</MudItem>
<MudItem xs="12" sm="6" md="4">
<MudAutocomplete T="string" Label="US States" @bind-Value="_value"
SearchFunc="@Search1" Variant="Variant.Outlined" />
</MudItem>
</MudGrid>
@code
string _value = null;
private void LoadFormData()
_value="New Mexico";
StateHasChanged();
private string[] states =
"Alabama", "Alaska", "American Samoa", "Arizona",
"Arkansas", "California", "Colorado", "Connecticut",
"Delaware", "District of Columbia", "Federated States of Micronesia",
"Florida", "Georgia", "Guam", "Hawaii", "Idaho",
"Illinois", "Indiana", "Iowa", "Kansas", "Kentucky",
"Louisiana", "Maine", "Marshall Islands", "Maryland",
"Massachusetts", "Michigan", "Minnesota", "Mississippi",
"Missouri", "Montana", "Nebraska", "Nevada",
"New Hampshire", "New Jersey", "New Mexico", "New York",
"North Carolina", "North Dakota", "Northern Mariana Islands", "Ohio",
"Oklahoma", "Oregon", "Palau", "Pennsylvania", "Puerto Rico",
"Rhode Island", "South Carolina", "South Dakota", "Tennessee",
"Texas", "Utah", "Vermont", "Virgin Island", "Virginia",
"Washington", "West Virginia", "Wisconsin", "Wyoming",
;
private async Task<IEnumerable<string>> Search1(string value)
// In real life use an asynchronous function for fetching data from an api.
await Task.Delay(5);
// if text is null or empty, show complete list
if (string.IsNullOrEmpty(value))
return states;
return states.Where(x => x.Contains(value, StringComparison.InvariantCultureIgnoreCase));
当您单击“加载表单数据”时,自动完成功能会预先填充该值。如果这不能解决您的问题,您需要创建一个http://try.mudblazor.com sn-p 并将其发布在此处,以便我们查看您的问题。只要确保你保持简单。
【讨论】:
以上是关于MudAutocomplete - 需要在编辑表单上绑定值的主要内容,如果未能解决你的问题,请参考以下文章