razor - ASP.NET MVC One to Many Database Table Values with List Attribute -
my first table is:
first table name: contacts
contactid (pk)
firstname
lastname
company
second table name: phones
contactid (fk)
phonetype
phonenumber
my view model is
public class contactvm2 { public int contactid { get; set; } public string firstname { get; set; } public string lastname { get; set; } public string company { get; set; } public string phonetype { get; set; } public string phonenumber { get; set; } }
repository class is
public class contactrepository { contactsdbentities dbrepo = new contactsdbentities(); public list<contactvm> getallcontacts() { list<contactvm> contactviewlist = new list<contactvm>(); var allcontacts = dbrepo.contacts.tolist(); var allphones = dbrepo.phones.tolist(); foreach (var cont in allcontacts) { foreach (var ph in allphones) { if (cont.contactid == ph.contactid) { contactviewlist.add(new contactvm(){ contactid =cont.contactid, firstname=cont.firstname, lastname=cont.lastname, company=cont.company, phonetype=ph.phonetype, phonenumber=ph.phonenumber}); } } } return contactviewlist; } }
and controller is
public actionresult index() { contactrepository contrepo = new contactrepository(); var allcontacts = contrepo.getallcontacts().tolist(); return view(allcontacts); }
i have following data in contacts tables
contactid firstname lastname company
1 bill gates microsoft
and in phones table
contactid phonetype phonenumber
1 home 1111
1 office 2222
i getting following result
1 bill gates home 1111
1 bill gates office 2222
where contact details repeating.
need following result
1 bill gates home 1111
office 2222
have tried following changes in view
<td style="border:2px solid blue;"> @{ foreach (var parent in model.where(x=>x.contactid==item.contactid).groupby(m=>m.phonenumber)) { foreach( var itm in parent ) { @itm.phonenumber <br /> } } } </td> <td style="border:2px solid red;"> @{ foreach (var parent in model.where(x=>x.contactid==item.contactid).groupby(m=>m.phonetype)) { foreach( var itm in parent ) { @itm.phonetype <br /> } } } </td>
but still repeats record again.
tried following changes in modelview
public list<string> phonetype { get; set; } public list<string> phonenumber { get; set; }
but did not results.
can 1 giving simplest example @ beginner level. code without phone iteration in view
@foreach (var item in model) { <tr> <td> @html.displayfor(modelitem => item.contactid) </td> <td> @html.displayfor(modelitem => item.firstname) </td> <td> @html.displayfor(modelitem => item.lastname) </td> <td> @html.displayfor(modelitem => item.company) </td> <td> @html.displayfor(modelitem => item.phonetype) </td> <td> @html.displayfor(modelitem => item.phonenumber) </td> <td> @html.actionlink("edit", "edit", new { /* id=item.primarykey */ }) | @html.actionlink("details", "details", new { /* id=item.primarykey */ }) | @html.actionlink("delete", "delete", new { /* id=item.primarykey */ }) </td> </tr>
you should remove iteration repository class , create separate class phone number , add variable of class in contactvm2 the below model:
public class contactvm2 { public int contactid { get; set; } public string firstname { get; set; } public string lastname { get; set; } public string company { get; set; } public list<contactvm2phone> phonelist {get; set;} } public class contactvm2phone { public string phonetype { get; set; } public string phonenumber { get; set; } }
repository class
public class contactrepository { contactsdbentities dbrepo = new contactsdbentities(); public list<contactvm> getallcontacts() { list<contactvm> contactviewlist = new list<contactvm>(); var allcontacts = dbrepo.contacts.tolist(); var allphones = dbrepo.phones.tolist(); foreach (var cont in allcontacts) { contactviewlist obj= new contactvm() obj.contactid =cont.contactid; obj.firstname=cont.firstname; obj.lastname=cont.lastname; obj.company=cont.company; list<contactvm2phone> phonelist= new list<contactvm2phone>(); foreach (var ph in allphones) { if (cont.contactid == ph.contactid) { phonelist.add(new contactvm2phone(){ phonetype=ph.phonetype, phonenumber=ph.phonenumber}); } } obj.phonelist =phonelist; contactviewlist.add(obj); } return contactviewlist; } }
Comments
Post a Comment