Mvc C# Html.dropdownlist And Viewbag
So I have the following (pseudo code): string selectedvalud = 'C'; List list= new List(); foreach(var item in mymodelinstance.Codes){
Solution 1:
Try like this:
ViewBag.ListOfCodes = new SelectList(mymodelinstance.Codes, "Id", "Name");
ViewBag.Codes = "C";
and in your view:
<%= Html.DropDownList(
"Codes",
(IEnumerable<SelectListItem>)ViewBag.ListOfCodes,
new { style = "max-width: 600px;" }
) %>
For this to work you obviously must have an item with Id = "C" inside your collection, like this:
ViewBag.ListOfCodes = new SelectList(new[]
{
new { Id = "A", Name = "Code A" },
new { Id = "B", Name = "Code B" },
new { Id = "C", Name = "Code C" },
}, "Id", "Name");
Post a Comment for "Mvc C# Html.dropdownlist And Viewbag"