MVC @Html.DropDownList - Default Value

12,824

The code is sound, but I think in order for the scope of the controller to appropriately communicate with your view, you need to do the:

@Html.DropDownListFor(model => model.PayList)

Actually... here's a good article: dropdownlist set selected value in MVC3 Razor

Don't use the ViewBag... get familiar with creating ViewModels.

Share:
12,824
Nate Pet
Author by

Nate Pet

Updated on June 04, 2022

Comments

  • Nate Pet
    Nate Pet almost 2 years

    I have a list which I created in the controller:

        var PayList = new[] {  
                      new ListEntry { Id = 1, Name = "" },       
                      new ListEntry { Id = 2, Name = "Yes" },         
                      new ListEntry { Id = 3, Name = "No" }          
    
                    };
    
        ViewBag.PayList = new SelectList(PayList, "Id", "Name",2); 
    

    In the View I have the following:

        @Html.DropDownList("Pay", ViewBag.PayList as SelectList) 
    

    I was expecting the above to default to Yes but did not happen(Note that I am passing 2 in the ViewBag.PayList.