Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DropDownList错误,System.InvalidOperationException_C#_Asp.net Mvc - Fatal编程技术网

C# DropDownList错误,System.InvalidOperationException

C# DropDownList错误,System.InvalidOperationException,c#,asp.net-mvc,C#,Asp.net Mvc,Create.cshtml @Html.DropDownList("KategoriId",null,htmlAttributes: new { @class="form-control"}) MakaleController.cs public ActionResult Create() { ViewBag.KategoriId = new SelectList(db.Kategoris, "KategoriId", "KategoriAdi");

Create.cshtml

@Html.DropDownList("KategoriId",null,htmlAttributes: new { @class="form-control"})
MakaleController.cs

public ActionResult Create()
        {
            ViewBag.KategoriId = new SelectList(db.Kategoris, "KategoriId", "KategoriAdi");
            return View();
        }
但据说当我提出发帖请求时

具有“KategoriId”键的ViewData元素的类型为“System.Int32”,但类型为“IEnumerable”

如何修复这个错误,我尝试了很多方法,但都没有发生。他说这个值通常为空

cs(!!不是KATEGORIS,我先用代码创建)

已更新 您的下拉列表定义不正确

它应该如下所示

@Html.DropDownListFor(m => m.CategoryID, Model.CategoryList, "-Please select-")
您将null传递给Dropdownlist,但它至少应该是一个空列表 您还尝试在viewbag中保留dropdownlist数据。它应该是视图模型中的列表属性。 注意:由于可读性问题,在全局项目中不允许在变量名中使用本机单词=)


附言:你可以在这里查看类似的问题和答案

这个项目不是全球性的,我只是在学习mvc:)thnx,但它再次给出同样的错误(
@Html.DropDownListFor(m => m.CategoryID, Model.CategoryList, "-Please select-")