c# - MVC returning entity with foreign key, again -


i'm trying return entity has foreign key , i'm bit stuck. trying populate dropdownlist results of 2 tables. there's foreign key relationship between itemtype , indicatortype tables.

code is:

public async task<pagedresults<itemtype>> getasync(int skip = 0, int take = -1) {     pagedresults<itemtype> pagedresults;     try     {         iqueryable<itemtype> query = _dbset.include("indicatordetails");             query = take == -1             ? query.orderby(i => i.indicatordetails.code)             : query.orderby(i => i.indicatordetails.code);             var data = await query.tolistasync();              pagedresults = new pagedresults<itemtype>(data, skip, take, data.count);     }     catch (exception e)     {         throw new exception(string.format("could not item types database. {0}", e.message), e);     }     return pagedresults; } 

i can work returning single row, because i'm passing in id can match between itemtype , indicatortype classes can't figure out how _dbset include indicatordetails.

thanks in advance.

you can query joining both table exemple (j1 itemtype , j2 indicatortype)

viewbag.listitems = itemtype.join(indicatortype, j1 => j1.key, j2 => j2.key2, (j1, j2) => new {j1, j2}).asenumerable().select( v => new selectlistitem() { value = t.t1.userid.tostring(), text = string.format("{0} {1}", t.t1.firstname, t.t2.lastname) }); 

in view can pass viewbag.listitems

@html.dropdownlistfor(m => m.userid, new selectlist(viewbag.listitems,"value","text","-sélections-"), new { @class ="form-control"}) 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -