Asp.Net MVC 2 Dropdown Displaying System.Web.MVC.SelectListItem

后端 未结 1 936
青春惊慌失措
青春惊慌失措 2021-01-17 17:05

I have a table that contains a list of EquipmentIDs and another table that has maintenance records.

When the user edits a maintenance record I want there to be a d

1条回答
  •  礼貌的吻别
    2021-01-17 17:32

    SelectList and SelectListItem are actually mutually exclusive. You should be using one or the other. Etiher pass the constructor of SelectList your raw data (IDs) or don't use SelectList at all and just make ViewData["EquipIDs"] your enumerable of SelectListItem. If you go with the latter approach, you will have to tweak your code so that you are setting the selected item in the constructor of SelectListItem (as you had done, but commented out).

    Either:

    ViewData["EquipIDs"] = new SelectList(IDs, maintPerformed.ID, "EquipID", "Sort");
    

    Or:

    IEnumerable selectEquipList =
        from c in IDs
        select new SelectListItem
        {
            Selected = c.EquipID == maintPerformed.EquipID,
            Text = c.EquipID,
            Value = c.Sort.ToString()
        };
    
    ViewData["EquipIDs"] = selectEquipList;
    

    0 讨论(0)
提交回复
热议问题