Reputation: 51
I am trying to bind a multiple select to a value which is then passed to a model, but currently it is only returning one value, I tried changing it from a string to a string array but got many errors and couldn't find a solution.
Does anyone know how I can return all the values the user has selected?
Thank You!
<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();
}
}
}
}
Upvotes: 4
Views: 14678
Reputation: 54
<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; }
}
}
Upvotes: 3