2016-04-25 182 views
0

我在這裏遇到了問題,我希望有人能幫助我解決我的問題。問題是:即時通訊使用C#和MVC。我想將db表傳遞給另一個函數。我不知道如何解決這個..希望有人在那裏可以幫助我將查詢表​​傳遞給mvc中的另一個函數c#

public ActionResult Index() 
    { 
     var viewModel = db.test.ToList() 
     ExportExcel(viewModel); //pass current table to excel to export 
     return View(viewModel); 
    } 

//export to excel 
public ActionResult ExportExcel(xxxx viewModel) 
    { 
     var grid = new GridView(); 
     grid.DataSource = from 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"); 
    } 
+1

究竟有什麼問題了嗎?什麼不起作用? –

+0

我想將索引()中的viewModel傳遞給ExportExcel()..但我不知道如何調用它..請參閱ExportExcel'xxxx' – achimet

+0

將xxxx更改爲'viewModel'的類型? – Shyju

回答

0

由於View("viewName")返回查看結果,您可以直接調用它並從Index方法返回它,前提是您將viewModel傳遞給您的方法ExportExcel

public ActionResult Index() 
{ 
    var viewModel = db.test.ToList() 
    var viewResult = ExportExcel(viewModel); 
    return viewResult; 
} 

//export to excel 
public ActionResult ExportExcel(List<test> viewModel) 
{ 
    var grid = new GridView(); 
    grid.DataSource = from 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"); 
} 
+0

其說的類型或命名空間名稱'測試'找不到(你是否缺少使用指令或程序集引用?) – achimet

+0

對不起我的錯誤..我需要替換模型與我的真實模型..對不起.. – achimet

+0

@achimet沒問題。這僅僅是你的模型可能的最佳猜測。如果你更新你的問題,我會相應地更新我的答案。 –

0

更改ExportExcel函數返回類型爲布爾或作廢,並改變參數到視圖模型,而不是XXXX .. 也許你可以移動代碼到一些實用程序類。

+0

public void ExportExcel(viewmodel viewModel)?? – achimet

+0

是..還有瀏覽模式意味着列表 .. –

+0

抱歉.. u能解釋更詳細 我得到這個錯誤: 類型或命名空間名稱「視圖模型」找不到 – achimet

0

假設視圖模型是一個System.Data.DataTable:

public ActionResult Index() 
    { 
     // Dummy data 
     var dt = new DataTable(); 
     dt.Columns.Add("id", typeof(int)); 
     var row = dt.NewRow(); 
     row[0] = 4; 
     dt.Rows.Add(row); 

     ExportExcel(dt); 
     return View(); 
    } 

在您的導出方法改變paprameter類型數據表:

public EmptyResult ExportExcel(DataTable test) 
    { 
     var grid = new GridView(); 

     grid.DataSource = test; 
     grid.DataBind(); 
     ... your existing code 
     return new EmptyResult(); 
    } 
相關問題