asp:DropDownList oder Html.DropDownList()

Augus

Grünschnabel
Hallo zusammen,

Ich möchte eine DropDownList erstellen, die mit Daten gefüttert wird. Wählt der Benutzer einen Eintrag aus der DropDownList soll das enthaltene Objekt zurückgeliefert werden.

Ich hab beides mal im Code versucht. Bin aber leider immer noch ratlos was ich nehmen kann, dass ich das angebene Objekte wieder zurück bekommen.

Für das Erstere:
In der aspx Datei (View):

Code:
<%= Html.DropDownList("person2") %>

In der cs Datei (Controller):
Code:
            List<string> ListPersons = new List<string>();
            int j = 0;
            for(int i=1; i <= MvcApplication.dgObject.CountDgPersons(); i++) 
            {
                ListPersons.Add(MvcApplication.dgObject.GetDgPerson(j).Firstname + MvcApplication.dgObject.GetDgPerson(j).Surname);
                j++;
            }

            ViewData["person2"] = new SelectList(ListPersons);

Für das Zweite:
In der aspx Datei (View):

Code:
          <asp:DropDownList ID="DropDownList1" runat="server" 
              DataSourceID="ObjectDataSource1" DataTextField="Firstname" 
              AutoPostBack="True" 
              onselectedindexchanged="DropDownList1_SelectedIndexChanged">
          </asp:DropDownList>

        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
              SelectMethod="getDgObject" 
              TypeName="DistGen_GUI_MVC.Controllers.HomeController">
          </asp:ObjectDataSource>

Die SelectMethod:

Code:
        public ICollection getDgObject()
        {
            List<DgPerson> dgPerson = new List<DgPerson>();
            int j=0;
            for (int i = 0; i < MvcApplication.dgObject.CountDgPersons(); i++)
            {
                dgPerson.Add(MvcApplication.dgObject.GetDgPerson(i));
                j++;
            }
            return dgPerson;
        }
 
Zurück