asp.net mvc 4 - MVC4 C# Populating data in a viewmodel from database -


i have viewmodel needs data 2 models person , address:

models:

public class person {    public int id { get; set; }    public string name { get; set; }    public int age { get; set; }    public int gender { get; set; } }  public class address {    public int id { get; set; }    public string street { get; set; }    public int zip { get; set; }    public int personid {get; set; } } 

the viewmodel such

public class personaddviewmodel {     public int id { get; set; }     public string name { get; set; }     public string street { get; set; } } 

i have tried several ways data viewmodel , pass view. there multiple records returned display.

my latest method populating view model such:

private appcontexts db = new appcontexts(); public actionresult listpeople() {     var model = new personaddviewmodel();     var people = db.persons;     foreach(person p in people)     {         address address = db.addresses.singleordefault(a => a.personid == p.id)         model.id = p.id;         model.name = p.name;         model.street = address.street;     }     return view(model.tolist()); } 

i error on address address = db... line of "entitycommandexecutionexception unhandled user code.

how can populate view model multiple records , pass view?

final solution:

private appcontexts db = new appcontexts(); private appcontexts dbt = new appcontexts(); public actionresult listpeople() {     list<personaddviewmodel> list = new list<personaddviewmodel>();     var people = db.persons;     foreach(person p in people)     {         personaddviewmodel model = new personaddviewmodel();         address address = dbt.addresses.singleordefault(a => a.personid == p.id)         model.id = p.id;         model.name = p.name;         model.street = address.street;     }     return view(list); } 

first, entitycommandexecutionexception errors indicates error in definition of entity context, or entities themselves. throwing exception because it's found database different way told it should be. need figure out problem.

second, regarding proper way this, code you've shown should work if context correctly configured. but, better way use navigational properties, long want related records , not specify other clause parameters. navigational property might this:

public class person {    public int id { get; set; }    public string name { get; set; }    public int age { get; set; }    public int gender { get; set; }     public virtual address address { get; set; }    // or possibly, if want more 1 address per person    public virtual icollection<address> addresses { get; set; } }  public class address {    public int id { get; set; }    public string street { get; set; }    public int zip { get; set; }    public int personid { get; set; }     public virtual person person { get; set; } } 

then say:

public actionresult listpeople() {     var model = (from p in db.persons // .includes("addresses") here?                 select new personaddviewmodel() {                     id = p.id,                     name = p.name,                     street = p.address.street,                     // or if collection                     street2 = p.addresses.select(a => a.street).firstordefault()                 });      return view(model.tolist()); } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -