I'm populating DropdownList
with data retrieved from database. I want to add default empty string. I'm trying with SelectedValue property
with no effects. I tried already null
and string.empty
like in sample below
ViewBag.Project = new SelectList(unitOfWork.projectRepository.Get(), "ProjectName", "ProjectName",string.Empty);
DropDownList declaration in View
@Html.DropDownList("Project", null, new { @class="form-control"})
which translates into:
<select class="form-control" id="Project" name="Project">
<option value="Sample project">Sample project</option>
<option value="Second project">Second project</option>
</select>
By there is no option with String.Empty.
What should I change in my code
As @Alexander suggested I converted it with ToList and now this is my code:
var items = unitOfWork.projectRepository.Get();
items=items.ToList<Project>().Insert(0, new Project { ProjectId = 1, ProjectName = "" });
ViewBag.Project = new SelectList(items, "ProjectName", "ProjectName",string.Empty);
but this throws exception:Error Cannot implicitly convert type 'void' to 'System.Collections.Generic.IEnumerable<magazyn.Models.Project>