Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.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# 没有类型为';IEnumerable<;选择列表项>';那有钥匙';选定的分类ID';_C#_Asp.net Mvc_Razor - Fatal编程技术网

C# 没有类型为';IEnumerable<;选择列表项>';那有钥匙';选定的分类ID';

C# 没有类型为';IEnumerable<;选择列表项>';那有钥匙';选定的分类ID';,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我犯了那个错误 没有具有键“SelectedCategoriesID”的“IEnumerable”类型的ViewData项 我的型号代码: public class NewsViewModel { public int NewsId { get; set; } public string NewsTitle { get; set; } public string NewsBody { get; set; } publi

我犯了那个错误

没有具有键“SelectedCategoriesID”的“IEnumerable”类型的ViewData项

我的型号代码:

   public class NewsViewModel
    {
        public int NewsId { get; set; }
        public string NewsTitle { get; set; }
        public string NewsBody { get; set; }
        public System.DateTime NewsDate { get; set; }
        public string NewsImagePath { get; set; }
        public HttpPostedFileBase NewsImageFile { get; set; }
        public int[] SelectedCategoriesIds { get; set; }

    }
控制器中的我的代码:

var list = db.Categories.ToList();
ViewBag.CategoryList = from c in list 
                       orderby c.CategoryName 
                       select new SelectListItem 
                                 { 
                                   Value = c.CategoryId.ToString(), 
                                   Text = c.CategoryName.Trim() 
                                 };
在Razor中:

@Html.DropDownListFor(model => model.SelectedCategoriesIds,
                     (IEnumerable<SelectListItem>)ViewBag.CategoryList,
                        new 
                        { 
                          id = "dropdownOne", 
                          @class = "form-control", 
                          multiple = "true" 
                         }
                      )
@Html.DropDownListFor(model=>model.selectedCategoriesID,
(IEnumerable)ViewBag.CategoryList,
新的
{ 
id=“dropdownOne”,
@class=“表单控制”,
multiple=“true”
}
)

DropDownList for仅支持一种选择。看起来您需要一个复选框列表,因为它是一个多选列表

你可以查看我的小nuget包CheckBoxList.Mvc,它为你提供CheckBoxListFor


目前它不支持int[],但我现在要添加它。

对于多个选择,您可以使用

  • @Html.ListBoxFor(x=>x.model.selectedCategoriesID,(IEnumerable)ViewBag.CategoryList,新的{id=“AnyName”,Multiple=“Multiple”})

展示您的模型class@EhsanSajjad我编辑了代码,非常感谢
selectedcategoriesid
应该是
int
not
int[]
如果int不是int[],我如何循环?我需要一个Idsdropdownlist数组返回单个值,该值被选中感谢您的帮助,但仍然是相同的错误