2016-12-02 4 views
0

Ich habe gerade mein Konto auf Stack-Überlauf erstellt, so ist dies das erste Mal, eine Frage zu stellen: Ich habe ein Problem mit meiner DropDownList. Ich weiß nicht, was ich falsch mache, da ich relativ neu bin auf ASP-NET und MVC 5, so entschuldigen Sie bitte alle schrecklichen Dinge, die ich tatDropDownListFor: Nein ViewData Element des Typs 'IEnumerable <SelectListItem>' mit dem Schlüssel

Controller:

public ActionResult typeDevices() 
    { 
     List<SelectListItem> typelist = new List<SelectListItem>(); 
     DropDownModel type = new DropDownModel(); 


     type.TypeDevicesId = 1; 
     type.TypeDevicesValue = "typeDevice1"; 

     typelist.Add(new SelectListItem() { Value = type.TypeDevicesValue, Text = type.TypeDevicesId.ToString() }); 

     type.TypeDevicesId = 2; 
     type.TypeDevicesValue = "typeDevice2"; 

     typelist.Add(new SelectListItem() { Value = type.TypeDevicesValue, Text = type.TypeDevicesId.ToString() }); 

     type.TypeDevicesId = 3; 
     type.TypeDevicesValue = "typeDevice3"; 

     typelist.Add(new SelectListItem() { Value = type.TypeDevicesValue, Text = type.TypeDevicesId.ToString() }); 

     SelectList listvalues = new SelectList(typelist, "Text", "Value"); 
     ViewBag.DropDownValues = listvalues; 
     ViewData["DropDownData"] = new SelectList(listvalues, "Text", "Value"); 
     return View(); 
    } 

Modell:

public class DropDownModel 
{ 
    public int TypeDevicesId { get; set; } 
    public string TypeDevicesValue { get; set; } 
} 

Index:

<p> 
    <label class="field" for="Type">Type:</label> 
    @Html.DropDownList("DropDownData", (SelectList)ViewBag.DropDownValues)) 
</p> 

Dieser Code stammt aus einem Video, das ich folgte. Link to video

Der Fehler, der ich bin, ist immer

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'DropDownData'

+0

Das Erstellen einer zweiten identischen 'SelectList' von der ersten ist sinnloser zusätzlicher Overhead - es ist nur' ViewBag.DropDownValue = typelist; 'Und Sie können keine'