c# - How to supply dynamic data set to a single view using ViewModels -


(first time posting , new mvc) have mvc/c# application user selects list of id's , chooses report type (8 possible different reports calling 8 different database views). passing of these parameters controller. based on report type chosen, want fetch data, send view , display data in grid/table format.

i tried sending data via viewbag , viewdata had trouble parsing actual columns/data using @foreach. when decided try viewmodels.

//my viewmodel.... //i know missing {get;set;} code // need with.  need pass selected id's each database view ,  // perform additional query requests (i.e. distinct, order by).  // sample query: // var dd = (from p in _db.report1 select new { sid = p.reportid, username = p.submitted_by,  //      balance = p.totaldebt}).distinct(); // dd = dd.where(w => chosenuserids.contains(w.sid)); // dd.orderby(p => p.sid).thenby(p => p.username).thenby(p => p.balance);  public class userreportsviewmodel {     public list<namespace.report1> report1 = new list<namespace.report1>();     public list<namespace.report2> report2 = new list<namespace.report2>();     public list<namespace.report3> report3 = new list<namespace.report3>();     ... }  //my controller userreportsviewmodel userreportvm = new userreportsviewmodel(); switch (reporttype) {     case "report1":         //pass selected id's , data report1 db view         // not quite sure how this.         break;     case "report2":         break;     case "report3":         break;     default:         break; } return view(userreportsvm); 

am on right track? came across partial views , having view call/referrence correct partial view (?). older languages lot simpler accomplish liking mvc/.net/c#.

as database, using codefirst entity framework.

i highly suggest have view, viewmodel , action each report. call correct endpoint based on requested report in client.

or, if want 1 endpoint (action) specify view want return. if don't specify view name, returns view based on action name, can specify view render.

return view("viewtorender", viewmodel); 

if want fancy, 1 view. include layout object in viewmodel data... way can use 1 view model. layout list of "column info fields". column1 have header, width, etc. bound genericreportviewmodel.column1.

from there, need project data generic view model.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -