2

This may be a stupid question but I do not know why this is the default behavior for the Select TagHelper.

This is what I have in my view

<select asp-for="Estimators" asp-items="Model.Estimators" class="form-control"></select>

and this is the output on the page

<select class="form-control" id="Estimators" multiple="multiple" name="Estimators"><option value="2">Enio LastName</option>
<option value="6">Ianko Diaz</option>
<option value="7">Iordan Diaz</option>
<option value="8">Joan Alonso</option>
<option value="5">Lazaro Araya</option>
<option value="3">Leydis Martinez</option>
<option value="4">Ruben Cruz</option>
<option value="1">Shamir Ajate</option>
<option value="9">Yudiel Curbelo</option>
</select>

why is the select tag rendering as multiple="multiple".

Yudiel Curbelo
  • 116
  • 1
  • 9

1 Answers1

7

You are actually using the helper wrong. You should have another property to store the selected item.

public class YourViewModel
{
   public int SelectedEstimator { set; get; }
   public List<SelectListItem> Estimators { set; get; }
}

And in your view

@model YourViewModel
<select asp-for="SelectedEstimator" asp-items="@Model.Estimators">
    <option>Please select one</option>
</select>

This will render a single selectable SELECT element.

When the property you use for asp-for items is of array type, the generated select element will be multi select.

public class YourViewModel
{
   public int[] SelectedEstimator { set; get; }
   public List<SelectListItem> Estimators { set; get; }
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Here is my property. public IEnumerable Estimators { get; set; } – Yudiel Curbelo Jan 31 '16 at 04:22
  • @YudielCurbelo You are using the helper in a wrong way. See my answer which explains how to use it. Also see [this](http://stackoverflow.com/questions/34624034/select-tag-helper-in-mvc-6/34624217#34624217) answer for more detailed sample. – Shyju Jan 31 '16 at 04:27
  • 2
    @YudielCurbelo You may have forgotten to mark this response from @Shyju as `Answer`. You may want to do that for the benefit of other users. – nam Nov 08 '16 at 20:01