gpt4 book ai didi

c# MVC Dropdownlist - 没有具有键 'IEnumerable' 的 ' ' 类型的 ViewData 项

转载 作者:行者123 更新时间:2023-11-30 15:28:36 26 4
gpt4 key购买 nike

我只是试图通过在 home/contact.cshtml 上添加一个 DropDownList 来扩展以前的项目。

我的问题是我在 firefox 中加载页面时不断收到以下错误

错误:System.Web.Mvc.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理不存在具有键“displayGraph”的“IEnumerable”类型的 ViewData 项

我在另一个页面上有另一个工作正常的下拉列表(相同的方法),如果我将相同的代码复制到一个新项目中它工作正常,谁能告诉我是什么原因导致的?

contact.cshtml - 代码片段

    @using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
<p>
Filter By: @Html.DropDownList("displayGraph","Select a Graph")
<input type="submit" value="Filter" />
</p>
}

HomeController - 代码片段

    public ActionResult Index()
{
string chart1 = "Num Each Model Processed", chart2 = "Another chart to be assigned";
var GraphLst = new List<string> { chart1, chart1 };
ViewBag.displayGraph = new SelectList(GraphLst);

string userName = User.Identity.Name;
return View();
}

Graphdropdownmodel - 代码片段

   namespace TestSolution.Models
{
public class GraphDropdownModel
{
public IEnumerable<SelectListItem> Graph{ get; set; }
}
public class GraphDBContext : DbContext
{
public DbSet<GraphDropdownModel> Graphs { get; set; }
}
}

最佳答案

尝试使用@Html.DropDownListFor

    @Html.DropDownListFor(model => model.value, (SelectList)ViewBag.displayGraph)

关于c# MVC Dropdownlist - 没有具有键 'IEnumerable<SelectListItem>' 的 ' ' 类型的 ViewData 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25408977/

26 4 0