I have a select List that is supposed to default the value saved from the model. However, it is not selecting the default value (checked the inspect element). I am using similar code for SelectLists in other places that work correctly.
Here is the code that I have that isn't working:
var techs = new HashSet<Guid>(db.usersInRoles.Where(u => u.RoleId == new Guid(Properties.Settings.Default.TechID)).Select(u => u.UserId));
ViewBag.TechnicianId = from user in db.Users
join tech in techs on user.UserId equals tech
where user.Status == 1
orderby user.FirstName
select new { TechnicianId = user.UserId, FullName = user.FirstName + " " + user.LastName };
and the view:
<span style="font-weight:normal;">@Html.DropDownListFor(m => m.TechnicianId, new SelectList(ViewBag.TechnicianId, "TechnicianId", "FullName"))</span>
After finding this Question and this Question, I modifyied my view to be like this:
<span style="font-weight:normal;">@Html.DropDownListFor(m => m.TechnicianId, new SelectList(ViewBag.TechnicianId, "TechnicianId", "FullName", @Model.TechnicianId))</span>
Unfortunately it is still not getting the default value from the model. I don't want to have to change my model to do something that should be so simple (as suggested in one of those links).
Here is the code that is working (and is the same to me):
ViewBag.Category = db.Categories.Where(c => c.Status == 1);
and the view:
<span style="font-weight:normal;">@Html.DropDownListFor(m =>
m.CategoryId, new SelectList(ViewBag.Category, "CategoryId", "CategoryName"))</span>