2

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.

dnatoli
  • 6,972
  • 9
  • 57
  • 96
Nate Pet
  • 44,246
  • 124
  • 269
  • 414

1 Answers1

1

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.

Community
  • 1
  • 1
richman64
  • 93
  • 8