views:

1105

answers:

2

I would like to assign a static list of items in a SelectList() to a Html.DropDownList() in ASP.NET MVC, what is the best practice?

I was about to try to find a way to use new SelectList(new {key = "value"}... but one, that didn't work, and two, would I be breaking a law here, should my static list be declared in ViewData anyway and passed as IList/IENumerable?

+1  A: 

OK I decided to take my own advice, and this should be defined in the controller:

FYI, I just returned:

PageData data = new PageData()
           {
               Formats = new[]
                             {
                                 new { ID = "string", Name = "Text" },
                                 new { ID = "int", Name = "Numeric" },
                                 new { ID = "decimal", Name = "Decimal" },
                                 new { ID = "datetime", Name = "Date/Time" },
                                 new { ID = "timespan", Name = "Stopwatch" }
                             },
               .............

           };
return View(data);

... (ignore context) and in the View ASPX side:

<%= Html.DropDownList("type.field", new SelectList(ViewData.Model.Formats, "ID", "Name"...

If anyone has a more optimal way of doing this I'll be happy to accept their answer.

GONeale
I have selected my answer as it is the one I went with.
GONeale
+6  A: 

It is a best practice not to create the SelectList in the view. You should create it in the controller and pass it using the ViewData.

Example:

  var list = new SelectList(new []
                                          {
                                              new {ID="1",Name="name1"},
                                              new{ID="2",Name="name2"},
                                              new{ID="3",Name="name3"},
                                          },
                            "ID","Name",1);
            ViewData["list"]=list;
            return View();

you pass to the constratctor: the IEnumerable objec,the value field the text field and the selected value.

in the View:

 <%=Html.DropDownList("list",ViewData["list"] as SelectList) %>
Marwan Aouida
agreed with Marwan. Create a SelectList object and pass that to your View. Personally, I would have strongly-typed views, but that is out of the scope of this question.
Pure.Krome
I use a strongly-typed view, why would you define the SelectList in the view? It's a 'view-particular' thing? I think it would be better to simply expose the data, that is what you are meant to give a view, data not context and data.?
GONeale
Actually I think it is very wrong, SelectList is used for rendering a dropdownlist which is totally UI specific. What if I now wanted to use this data for a check box list or something which doesn't support SelectList?
GONeale
I think it is better to define your SelectList in the controller to keep your view clean and contain as less code as possible.If you check the Nerd Dinner application, you will see it did the same thing.
Marwan Aouida
OK Marwan, I still am meaning to check that out. However I want to make sure I don't just favour cleanness over correctness.
GONeale