开发者

System.Web.MVC.SelectList how to set selected item upon creation

开发者 https://www.devze.com 2023-02-17 19:04 出处:网络
trying to have an item in a DropDownList selected by default from within my viewmodel. I am creating the select list in my view mod开发者_运维技巧el using teh following overload..

trying to have an item in a DropDownList selected by default from within my viewmodel.

I am creating the select list in my view mod开发者_运维技巧el using teh following overload..

public SelectList ProgramsSelectList() 
    {
        var ddlist = new SelectList(DiversionPrograms, "DiversionProgramID", "CrimeName", new {value = "1"});
        return ddlist;
    }

value = 1 is just hard coded for now. I am not sure the Html.DropDownList() respects this selected item.

Razor View:

 @Html.DropDownList("programSelect", Model.ProgramsSelectList(), "Choose a Program to Manage")

I must be missing something here...


Figured it out...

var ddlist = new SelectList(DiversionPrograms, "DiversionProgramID", "CrimeName", DiversionProgram.DiversionProgramID);
0

精彩评论

暂无评论...
验证码 换一张
取 消