MVC DropDownList SelectedValue not displaying correctly

后端 未结 7 958
甜味超标
甜味超标 2020-12-01 12:40

I tried searching and didn\'t find anything that fixed my problem. I have a DropDownList on a Razor view that will not show the the item that I have marked as Selected in th

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 12:59

    My solution was this... Where the current selected item is the ProjectManagerID.

    View:

    @Html.DropDownList("ProjectManagerID", Model.DropDownListProjectManager, new { @class = "form-control" })
    

    Model:

    public class ClsDropDownCollection
    {
       public List DropDownListProjectManager { get; set; }
       public Guid ProjectManagerID { get; set; }
    }
    

    Generate dropdown:

    public List ProjectManagerDropdown()
    {
        List dropDown = new List();
        SelectListItem listItem = new SelectListItem();
    
        List tempList = bc.GetAllProductManagers();           
    
        foreach (ClsProjectManager item in tempList)
        {
            listItem = new SelectListItem();
            listItem.Text = item.ProjectManagerName;
            listItem.Value = item.ProjectManagerID.ToString();
            dropDown.Add(listItem);
        }
        return dropDown;
    }
    

提交回复
热议问题