c# - There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'SelectedCategoriesIds' -


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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -