C# 没有类型为';IEnumerable<;选择列表项>';那有钥匙';选定的分类ID';
我犯了那个错误 没有具有键“SelectedCategoriesID”的“IEnumerable”类型的ViewData项 我的型号代码: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
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”})
selectedcategoriesid
应该是int
notint[]
如果int不是int[],我如何循环?我需要一个Idsdropdownlist数组返回单个值,该值被选中感谢您的帮助,但仍然是相同的错误