具有关键字“XXX”的ViewData项的类型为“System.String”,但必须为“IEnumerable<SelectListItem>”。

13

我对MVC还比较陌生。当我尝试提交一个包含静态DropDownListFor的表单时,出现以下异常:

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);
}
1个回答

26

具有键“XXX”的ViewData项的类型为“System.String”,但必须为“IEnumerable”类型。

通常情况下,当您将表单提交到一个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 元素所需的集合。

1
谢谢,这个可行! - justiceorjustus
不,应该是HttpPost。现已更正。 - Shyju

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接