2016-09-14 59 views
2

對於MVC來說很新穎。嘗試提交包含靜態DropDownListFor的表單時出現以下異常。具有鍵'XXX'的ViewData項目的類型爲'System.String',但必須是'IEnumerable <SelectListItem>'的類型。

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> 

型號:

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

查看:

<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> 

控制器:

[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(); 
} 

我已經嘗試了幾種不同的方法,只能似乎得到這個如果我直接填充列表項,則工作在靜態視圖中,我不覺得正確,因爲我將在此視圖中再次使用此列表。你怎麼看?

編輯:發佈操作。現在是空的。

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

回答

5

具有鑰匙 'XXX' 的類型 'System.String' 的,但必須是類型 '的IEnumerable' 的ViewData的項目。

通常當你提交表單的HTTP POST操作方法,你得到這個錯誤,裏面您無需重新加載需要渲染SELECT元素的數據返回相同的(視圖)模型回視圖

我猜你當前的代碼是這樣的

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

因爲我們的視圖代碼使用數據設置爲ViewData字典,你需要確保你所呼叫的return View()之前重裝ViewData["Positions"]

[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); 
} 

如果您不使用ViewData/ViewBag,但使用視圖模型,則需要執行相同操作。將視圖模型屬性重新加載到填充SELECT元素所需的集合。

+0

謝謝,這工作! – justiceorjustus

+0

它應該在HttpPost上。現在更正 – Shyju