Yudiel Curbelo
Yudiel Curbelo

Reputation: 116

MVC 6 - TagHelper Select

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".

Upvotes: 2

Views: 863

Answers (1)

Shyju
Shyju

Reputation: 218852

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

Upvotes: 7

Related Questions