2016-09-14 3 views
2

Ziemlich neu in MVC. Ich erhalte die folgende Ausnahme, wenn ich versuche, mein Formular zu senden, das eine statische DropDownListFor enthält.Das Element ViewData mit dem Schlüssel 'XXX' hat den Typ 'System.String', muss aber vom Typ 'IEnumerable <SelectListItem>' sein.

The ViewData item that has the key 'CurrentPosition' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The ViewData item that has the key 'CurrentPosition' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'. 

Source Error: 


Line 36:   @Html.LabelFor(m => m.CurrentPosition, new { @class = "col-md-2 control-label" }) 
Line 37:   <div class="col-md-10"> 
Line 38:    @Html.DropDownListFor(m => m.CurrentPosition, ViewData["Positions"] as SelectList) 
Line 39:    @Html.ValidationMessageFor(m => m.CurrentPosition, "", new { @class = "text-danger" }) 
Line 40:   </div> 

Modell:

[Display(Name = "Current Position")] 
[Required(ErrorMessage = "Please select their current position")] 
public string CurrentPosition { get; set; } 

Ausblick:

<div class="form-group"> 
    @Html.LabelFor(m => m.CurrentPosition, new { @class = "col-md-2 control-label" }) 
    <div class="col-md-10"> 
     @Html.DropDownListFor(m => m.CurrentPosition, ViewData["Positions"] as SelectList) 
     @Html.ValidationMessageFor(m => m.CurrentPosition, "", new { @class = "text-danger" }) 
    </div> 
</div> 

Controller:

[HttpGet] 
public ActionResult Sales() 
{ 
    var positions = new SelectList(new [] 
    { 
     new { value = 0, text = "Select a Position..." }, 
     new { value = 1, text = "Merchandiser" }, 
     new { value = 2, text = "ISR" }, 
     new { value = 3, text = "TM" }, 
     new { value = 4, text = "AISR" }, 
     new { value = 5, text = "TAM" }, 
     new { value = 6, text = "BAM" }, 
     new { value = 7, text = "CSR" }, 
     new { value = 8, text = "Director" }, 
     new { value = 9, text = "TSM" }, 
     new { value = 10, text = "Tel-Sell" }, 
     new { value = 11, text = "Graphics" }, 
     new { value = 12, text = "Shelf Set" }, 
     new { value = 13, text = "Secretary" }, 
     new { value = 14, text = "POS" }, 
     new { value = 15, text = "Other" } 
    }, 
    "value", "text", 1); 

    ViewData["Positions"] = positions; 

    return View(); 
} 

ich bereits verschiedene Möglichkeiten ausprobiert habe und nur diese zu bekommen scheinen funktionieren, wenn ich die Listenelemente direkt bevölkere in der Ansicht statisch, was ich nicht korrekt finde, da ich diese Liste in dieser Ansicht wieder verwenden werde. Was denken Sie?

Bearbeiten: Post Aktion. Für jetzt ist es leer.

[HttpPost] 
public ActionResult Sales(SalesViewModel model) 
{ 
    return View(model); 
} 

Antwort

5

Das Viewdata Element, das den Schlüssel ‚XXX‘ ist vom Typ ‚System.String‘ soll, muss aber vom Typ ‚IEnumerable‘ sein.

Normalerweise bekommen Sie eine Fehlermeldung, wenn Sie das Formular in eine HTTP-POST-Aktion-Methode einreichen und im Innern, dass Sie das gleiches zurückkehren (Ansicht) Modell zurück zur Ansicht , ohne die Daten Neuladen das SELECT-Element zu machen brauchen.

Ich denke, Ihre aktuellen Code so etwas wie dieses

[HttpPost] 
public ActionResult Sales(SomeViewModel model) 
{ 
    if(ModelState.IsValid) 
    { 
    // to do : Save and Redirect (PRG pattern) 
    } 
    return View(model); 
} 

Seit unserer Ansicht Code des Datensatzes verwendet, um Viewdata-Wörterbuch, Sie müssen sicherstellen, dass Sie Nachladen ViewData["Positions"] vor dem return View() Aufruf

ist.

[HttpPost] 
public ActionResult Sales(SomeViewModel model) 
{ 
    var positions = new SelectList(new [] 
    { 
     new { value = 0, text = "Select a Position..." }, 
     new { value = 1, text = "Merchandiser" }, 
     new { value = 8, text = "Director" } 
    },"value", "text", 1); 

    ViewData["Positions"] = positions; // Reloading the data here 
    return View(model); 
} 

Wenn Sie nicht mit Viewdata/ViewBag, sondern ein Ansichtsmodell verwenden, müssen Sie das gleiche tun. Laden Sie die Ansichtsmodelleigenschaft erneut in die Sammlung, die zum Auffüllen des SELECT-Elements benötigt wird.

+0

Danke, das hat funktioniert! – justiceorjustus

+0

Nein. Es sollte auf HttpPost sein. Jetzt korrigiert – Shyju

Verwandte Themen