gpt4 book ai didi

c# - Blazor 将多个选择绑定(bind)到一个值

转载 作者:行者123 更新时间:2023-12-04 07:32:54 24 4
gpt4 key购买 nike

我正在尝试将一个多选绑定(bind)到一个值,然后将该值传递给一个模型,但目前它只返回一个值,我尝试将它从一个字符串更改为一个字符串数组,但出现了很多错误并且不能找到解决方案。

有谁知道如何返回用户选择的所有值?

谢谢!

<div class="form-group col-md-6">
<label for="dur">Duration</label>
<select @bind="Duration" class="custom-select" id="dur" multiple>
<option value="12" selected>One Year</option>
<option value="24">Two Year</option>
<option value="36">Three Year</option>
<option value="48">Four year</option>
<option value="60">Five Year</option>
</select>
<small class="form-text text-muted">Hold <b>'CTRL'</b> to select multiple.</small>
</div>

@code {

private string _Duration;

private string Duration
{
get => _Duration;
set
{
if (value != _Duration)
{
_Duration = value;
UpdateModel();
}
}
}
}

最佳答案

<select multiple >
@foreach (var item in myVar)
{
<option value="@item.SlctValue" @onclick=@((e) => OptionClickEvent(@item.SlctValue,e))>@item.SlctName</option>
}
</select>

@foreach (var holderItem in myHolder)
{
@holderItem
}



@code {
private List<string> myHolder = new List<string>();

private List<SelectModel> myVar = new List<SelectModel>()
{
new SelectModel(){ SlctValue = 1, SlctName="One Year" },
new SelectModel(){ SlctValue = 2, SlctName="Two Year" },
new SelectModel(){ SlctValue = 3, SlctName="Three Year"},
new SelectModel(){ SlctValue = 4, SlctName="Four Year" },
};

public void OptionClickEvent(int values,MouseEventArgs evnt)
{
if (evnt.CtrlKey)
{
myHolder.Add(values.ToString());
}
}

public class SelectModel
{
public string SlctName { get; set; }
public int SlctValue { get; set; }
}

}

关于c# - Blazor 将多个选择绑定(bind)到一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67869897/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com