Believe me I have tried a lot of options to do that
and I have answer here
but I always look for the best practice and the best way I know so far for both front-end and back-end developers is for loop
(yes I'm not kidding)
Because when the front-end gives you the UI Pages with dummy data he also added classes and some inline styles on specific select option so its hard to deal
with using HtmlHelper
Take look at this :
<select class="input-lg" style="">
<option value="0" style="color:#ccc !important;">
Please select the membership name to be searched for
</option>
<option value="1">11</option>
<option value="2">22</option>
<option value="3">33</option>
<option value="4">44</option>
</select>
this from the front-end developer so best solution is to use the for loop
fristly create
or get your list
of data from (...) in the Controller Action and put it in ViewModel, ViewBag or whatever
//This returns object that contain Items and TotalCount
ViewBag.MembershipList = await _membershipAppService.GetAllMemberships();
Secondly in the view do this simple for loop to populate the dropdownlist
<select class="input-lg" name="PrerequisiteMembershipId" id="PrerequisiteMembershipId">
<option value="" style="color:#ccc !important;">
Please select the membership name to be searched for
</option>
@foreach (var item in ViewBag.MembershipList.Items)
{
<option value="@item.Id" @(Model.PrerequisiteMembershipId == item.Id ? "selected" : "")>
@item.Name
</option>
}
</select>
in this way you will not break UI Design, and its simple , easy and more readable
hope this help you even if you did not used razor