how to give a select a value using a model asp.net core code example

Example 1: select asp-items selecte one

@model MyViewModel
<form asp-controller="Home" asp-action="Create">

    <label>Comments</label>
    <input type="text" asp-for="Comments"/>

    <label>Lucky Employee</label>
    <select asp-for="EmployeeId" asp-items="@Model.Employees" >
        <option>Please select one</option>
    </select>

    <input type="submit"/>

</form>

Example 2: class selector to property in asp net core dropdown

<select data-val="true" data-val-required="The Employee field is required." id="Employee" name="Employee">    <option value=""></option>    <optgroup label="IT">        <option value="1">Mike</option>    </optgroup>    <optgroup label="Sales">        <option value="2">Pete</option>        <option value="4">Dean</option>    </optgroup>    <optgroup label="Admin">        <option value="3">Katy</option>    </optgroup></select>

Tags:

Misc Example