0

I'm using .net core MVC

I have loaded a dropdown with viewbag, but by default there is a selected value in the dropdownlist. I tried manually setting empty tag(<option selected="selected"></option>) but it didn't work still there another value getting selected.

Here is my code

@model Application.Services.PaginatedList<DataAccessViewModel>
@using System.Security.Claims;

@{
    ViewData["Title"] = "Index";
}
@if (User.IsInRole("Admin")) {
    <p>
        <a asp-action="Create">Create New Config</a>
    </p>
}
<form asp-action="Index" method="get">
    <div class="form-actions no-color">
        <p>
            Manager Name: <select class="form-check-inline" style="width:200px" name="managerCode" asp-for="@Model.FirstOrDefault().ManagerCode" asp-items="@(new SelectList(@ViewBag.Managers,"ManagerCode", "ManagerName"))">
                        <option selected="selected"></option>
                    </select>
        </p>
    </div>
</form>

Note, If I remove asp-for="@Model.FirstOrDefault().ManagerCode", by default empty is getting loaded but if I choose a managercode and move to next page the chosen managercode doesn't persist in dropdown. So I guess it is something to do with asp-for="@Model.FirstOrDefault().ManagerCode"

Pரதீப்
  • 88,697
  • 17
  • 124
  • 160
  • https://stackoverflow.com/questions/506001/asp-net-mvc-dropdown-with-a-default-empty-option hope this help you – Amal Ps Sep 14 '21 at 10:32

0 Answers0