asp.net mvc - pass query table to another function in mvc c# -


i got problem here , hope can assist me solve problem. problem is: im using c# , mvc. want pass db table function. dont know how solve this.. hope out there can me

public actionresult index()     {         var viewmodel = db.test.tolist()         exportexcel(viewmodel); //pass current table excel export         return view(viewmodel);     }  //export excel public actionresult exportexcel(xxxx viewmodel)     {         var grid = new gridview();         grid.datasource = p in viewmodel                           select new                           {                               name = p.name,                               no = p.staffno                           };         grid.databind();          response.clearcontent();         response.buffer = true;         response.addheader("content-disposition", "attachment; filename=myexcelfile.xls");         response.contenttype = "application/ms-excel";          response.charset = "";         stringwriter sw = new stringwriter();         htmltextwriter htw = new htmltextwriter(sw);          grid.rendercontrol(htw);          response.output.write(sw.tostring());         response.flush();         response.end();          return view("myview");     } 

since view("viewname") returns view result, can call directly , return index method, providing pass viewmodel exportexcel method.

public actionresult index() {     var viewmodel = db.test.tolist()     var viewresult = exportexcel(viewmodel);     return viewresult; }  //export excel public actionresult exportexcel(list<test> viewmodel) {     var grid = new gridview();     grid.datasource = p in viewmodel                       select new                       {                           name = p.name,                           no = p.staffno                       };     grid.databind();      response.clearcontent();     response.buffer = true;     response.addheader("content-disposition", "attachment; filename=myexcelfile.xls");     response.contenttype = "application/ms-excel";      response.charset = "";     stringwriter sw = new stringwriter();     htmltextwriter htw = new htmltextwriter(sw);      grid.rendercontrol(htw);      response.output.write(sw.tostring());     response.flush();     response.end();      return view("myview"); } 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -