c# - There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'SelectedCategoriesIds' -
this question has answer here:
i got error
there no viewdata item of type 'ienumerable' has key 'selectedcategoriesids'.
my model code:
   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; }      } my code in controller:
var list = db.categories.tolist(); viewbag.categorylist = c in list                         orderby c.categoryname                         select new selectlistitem                                   {                                     value = c.categoryid.tostring(),                                     text = c.categoryname.trim()                                   }; and in razor:
@html.dropdownlistfor(model => model.selectedcategoriesids,                      (ienumerable<selectlistitem>)viewbag.categorylist,                         new                          {                            id = "dropdownone",                            @class = "form-control",                            multiple = "true"                           }                       ) 
dropdownlistfor supports 1 selection. looks need checkboxlistfor since it's multiple choices.
you can check little nuget package checkboxlist.mvc provides checkboxlistfor
https://github.com/stephenzeng/checkboxlist.mvc
at moment not support int[] going add now.
Comments
Post a Comment