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.

3
  • 1
    What version of MVC? Your example code works fine for me in MVC3. Commented Nov 10, 2011 at 23:20
  • 1
    Check this answer: stackoverflow.com/questions/2410543/…. Commented Nov 10, 2011 at 23:20
  • This should work fine. In fact it worked fine when I tested it. Commented Nov 11, 2011 at 8:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.