Set selected value in SelectList after instantiation

后端 未结 10 2062
长发绾君心
长发绾君心 2020-12-05 17:39

Am I right to think that there is no way to set the selected value in the C# class SelectList after it is created? Isn\'t that a bit silly?

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 18:21

    The SelectList object is readonly after it was created. if you want to select something you better do it like:

    <%= Html.DropDownList("clientId", ViewData["clients"] as List,)%>
    

    And in the code behind:

        ViewData["clientId"] = "ASD"; //This should be the value of item you want to select
        ViewData["clients"] = clientItemList; //List
    

提交回复
热议问题