Multiple selection of ListBox is not working in asp.net mvc4 -


i have bound listbox data entity framework. have selected multiple values in listbox. but, values not fired. count of property 0 only. using code below:

public class sample1   {   [key]   public int sampleid{ get; set; }   public string sampledesc{ get; set; }   } public class expmodel   {   public list<sample1> sample{ get; set; }   } public actionresult index()   {        viewdata["samplelist"] = new list<sample1>(entity.samp);   return view();   }  @html.listboxfor(model => model.sample, new  selectlist(((list<details.models.sample1>)viewdata["samplelist"]), "sampleid", "sampledesc"))  

what have do? please me...

you should bind listboxfor helper property collection of simple/scalar values such strings or integers:

public class sample1   {       [key]       public int sampleid { get; set; }       public string sampledesc { get; set; }   }  public class expmodel   {       public list<int> selectedsampleids { get; set; }   } 

and then:

public actionresult index()   {            viewdata["samplelist"] = new list<sample1>(entity.samp);     return view();   } 

and in view:

@html.listboxfor(     model => model.selectedsampleids,      new selectlist(         (list<details.models.sample1>)viewdata["samplelist"],          "sampleid",          "sampledesc"     ) ) 

now when submit form, selectedsampleids collection contain selected ids in list box.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -