3

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();
                }
            }
        }
}
  • Checkout [this workaround](https://stackoverflow.com/a/60233404/15204525) that might help out. It covers how to bind to individual indices in an array which might work for you. – DekuDesu Jun 07 '21 at 11:20
  • What are the "many errors" you encountered ? Could you add the error(s) to the question to help clarify? – XouDo Jun 07 '21 at 11:52
  • @Joshua Bullock - this is a known problem with a live Issue on the AspNetCore team https://github.com/dotnet/aspnetcore/issues/5519. There's a workaround in the issue, there's Cem Erim answer below or you can change your approach and use say checkboxes. – MrC aka Shaun Curtis Jun 07 '21 at 14:13

1 Answers1

2
<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; }
   }

}
Matthias Müller
  • 444
  • 6
  • 15
Cem Erim
  • 29
  • 1
  • 1
    Welcome to Stack Overflow! Code-only answers are not particularly helpful. Please add some descriptions of how this code solves the problem. – Sven Eberth Jun 07 '21 at 21:13
  • 5
    as of net core 6 it should by possible by default: https://devblogs.microsoft.com/dotnet/asp-net-core-updates-in-net-6-preview-7/ – jerrevds Dec 09 '21 at 19:46