There is no ViewData item of type #39;IEnumerablelt;SelectListItemgt;#39; that has the key quot;keyquot;(没有“IEnumerablelt;SelectListItemgt;类型的 ViewData 项具有键“key的)
问题描述
我的代码是这样的
namespace DiagnosisApp.Models
{
public class ProcedurePrice
{
public int ID { get; set; }
public int DepartmentID { get; set; }
public int ProcedureID { get; set; }
public int InsuranceProviderID { get; set; }
public int ProcedureCategoryID { get; set; }
public int ProcedureSubCategoryID { get; set; }
[ForeignKey("ID")]
public virtual Department Department { get; set; }
[ForeignKey("ID")]
public virtual InsuranceProvider InsuranceProvider { get; set; }
[ForeignKey("ID")]
public virtual ProcedureCategory ProcedureCategory { get; set; }
[ForeignKey("ID")]
public virtual ProcedureSubCategory ProcedureSubCategory { get; set; }
}
}
控制器
public ActionResult Create()
{
ViewBag.DepartmentID = new SelectList(db.Departments, "ID", "Name");
ViewBag.ProcedureSubCategoryID = new SelectList(db.ProcedureSubCategories, "ID", "Name");
return View();
}
在我看来
@Html.DropDownList("ProcedureID", null, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ProcedureID)
在我看来一切正常,但它会引发错误
Everything looks okay to me, But it throws an error
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'ProcedureID'.
谁能指出我在这里做错了什么?
Can anyone point out what I am doing wrong here?
推荐答案
错误是你把它放在 ViewBag.ProcedureSubCategoryID
而你传递 ProcedureID
int Html.DropDownList()
并且您正在传递 SelectList
参数 null
.一个快速的解决方法是将 ProcedureSubCategoryID
替换为 Html.DropDownList()
中的 ProcedureID
作为第一个参数中的键:
The mistake is you are putting it in ViewBag.ProcedureSubCategoryID
while you are passing ProcedureID
int Html.DropDownList()
and also you are passing SelectList
parameter null
. A quick fix is to just replace ProcedureSubCategoryID
with ProcedureID
in Html.DropDownList()
as key in first parameter:
你有三种方法可以解决这个问题.
you have three ways to resolve this.
您可以这样做:
@Html.DropDownList("ProcedureID", ViewBag.ProcedureSubCategoryID as SelectList, new { @class="form-data" })
方式2:
public ActionResult Create()
{
ViewBag.DepartmentID = new SelectList(db.Departments, "ID", "Name");
ViewBag.ProcedureSubCategoryID = new SelectList(db.ProcedureSubCategories, "ID", "Name");
return View();
}
@Html.DropDownList("ProcedureSubCategoryID", null, new { @class = "form-control" })
方式3:
或替代方法是将其存储在您的操作中的 ProcedureID
中:
public ActionResult Create()
{
ViewBag.DepartmentID = new SelectList(db.Departments, "ID", "Name");
ViewBag.ProcedureID = new SelectList(db.ProcedureSubCategories, "ID", "Name");
return View();
}
在视图中:
@Html.DropDownList("ProcedureID", null, new { @class = "form-control" })
这篇关于没有“IEnumerable<SelectListItem>"类型的 ViewData 项具有键“key"的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:没有“IEnumerable<SelectListItem>"类型的 ViewData 项具有键“key"的


基础教程推荐
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01