gpt4 book ai didi

asp.net-mvc - 具有键 'XXX' 的 ViewData 项的类型为 'System.String',但必须为 'IEnumerable' 类型

转载 作者:行者123 更新时间:2023-12-02 02:44:55 24 4
gpt4 key购买 nike

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

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

我已经尝试了几种不同的方法,并且似乎只有在静态地直接在 View 中填充列表项时才能使其工作,但我发现这是不正确的,因为我将在此 View 中再次使用此列表。你觉得怎么样?

编辑:后操作。目前是空的。

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

最佳答案

The ViewData item that has the key 'XXX' is of type 'System.String' but must be of type 'IEnumerable'.

通常,当您将表单提交到 http post 操作方法并且在其中将相同的( View )模型返回到 View 而不重新加载呈现 SELECT 元素所需的数据时,您会收到此错误。

我猜你当前的代码是这样的

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

由于我们的 View 代码使用 ViewData 字典的数据集,因此您需要确保正在重新加载 ViewData["Positions"]在调用 return View() 之前.

[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,而是使用 View 模型,则需要执行相同的操作。将 View 模型属性重新加载到填充 SELECT 元素所需的集合。

关于asp.net-mvc - 具有键 'XXX' 的 ViewData 项的类型为 'System.String',但必须为 'IEnumerable<SelectListItem>' 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39496673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com