c# 4.0 - How to populate a ListBox based on a list of a model in asp.net mvc4? -


i have typed view have form based on model contact. in textboxes, default values of contact i'm passing view in controller. have class contact follows:

public class contact     {         public int idcontact { get; set; }         public string nom { get; set; }         public string prenom { get; set; }         public string email { get; set; }         public string fonction { get; set; }         public list<fonctioncontact> listefonctions = new list<fonctioncontact>();         public string telephonefixe { get; set; }         public string telephoneport { get; set; }         public contact() { }          public contact(int idcontact, string nom, string prenom, string email, string telephonefixe, string telephoneport)         {             this.idcontact = idcontact;             this.nom = nom;             this.prenom = prenom;             this.email = email;             this.telephonefixe = telephonefixe;             this.telephoneport = telephoneport;         }          public contact(int idcontact, string nom, string prenom, list<fonctioncontact> listefonctions, string email, string telephonefixe, string telephoneport)         {             this.idcontact = idcontact;             this.nom = nom;             this.prenom = prenom;             this.listefonctions = listefonctions;             this.email = email;             this.telephonefixe = telephonefixe;             this.telephoneport = telephoneport;         }     } 

there list of fonctioncontact. class of fonctioncontact:

public class fonctioncontact {     public int idfonction;     public string libellefonction;      public fonctioncontact() { }      public fonctioncontact(int idfonction, string libellefonction)     {         this.idfonction = idfonction;         this.libellefonction = libellefonction;     } } 

so display property listefonctions of contact in listboxfor doesn't work. there form i've tried display list :

@using (html.beginform()){       <label>nom:</label>      @html.textboxfor(contact => contact.nom, new { @value = @model.nom })      <label>prénom:</label>      @html.textboxfor(contact => contact.prenom, new { @value = @model.prenom })      ///the next controls next properties of class contact...      <label>fonction(s):</label>      @html.listboxfor(contact => contact.listefonctions, new selectlist(model.listefonctions, "idfonction", "libellefonction"));      } 

it shows me error: "model.fonctioncontact doesn't have property idfonction. i'm stuck here, can't find out what's wrong. has idea ?

basically, you'll need provide listboxfor list of value items (integers, use load selected , saved items). also, need multiselectlist second parameter (for explained reason, because it's not dropdownlist 1 selected item), neater compose in model, have written below:

model

public class contact         {             public int idcontact { get; set; }             public string nom { get; set; }             public string prenom { get; set; }             public string email { get; set; }             public string fonction { get; set; }              // save selection of items list             private list<int> _selectedfonctionids;                     public list<int> selectedfonctionids{                 {                     return _selectedfonctionids?? new list<int>();                 }                 set {                     _selectedfonctionids= value;                 }             }             private list<fonctioncontact> _listefonctions;             public multiselectlist listefonctions{                {                   return new multiselectlist(                             _listefonctions,                             "idfonction", // datavaluefield                             "libellefonction" // datatextfield                   );                    }             }             public string telephonefixe { get; set; }             public string telephoneport { get; set; }             public contact() { }              public contact(int idcontact, string nom, string prenom, string email, string telephonefixe, string telephoneport)             {                 this.idcontact = idcontact;                 this.nom = nom;                 this.prenom = prenom;                 this.email = email;                 this.telephonefixe = telephonefixe;                 this.telephoneport = telephoneport;             }              public contact(int idcontact, string nom, string prenom, list<int> selectedfonctionids, list<fonctioncontact> listefonctions, string email, string telephonefixe, string telephoneport)             {                 this.idcontact = idcontact;                 this.nom = nom;                 this.prenom = prenom;                 this.selectedfonctionids = selectedfonctionids;                 this._listefonctions = listefonctions;                 this.email = email;                 this.telephonefixe = telephonefixe;                 this.telephoneport = telephoneport;             }         } 

inside view's form

@html.listboxfor(contact => contact.selectedfonctionids, model.listefonctions) 

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 -