C# 在asp.net mvc中导出Excel工作表中的表视图

C# 在asp.net mvc中导出Excel工作表中的表视图,c#,excel,asp.net-mvc,C#,Excel,Asp.net Mvc,我想导出Razor视图表的Excel表。此代码显示以下表格: public ActionResult Show(int id) { IEnumerable<GradeSheetViewModel> model = _repGrade.GetList(id); return View(model); } 但它给出了一个错误 gv.DataSource = this.Show(); 错误是 方法Show没有重载接受0参数 DataSou

我想导出Razor视图表的Excel表。此代码显示以下表格:

public ActionResult Show(int id)
    {
        IEnumerable<GradeSheetViewModel> model = _repGrade.GetList(id);
        return View(model);
    }
但它给出了一个错误

gv.DataSource = this.Show();
错误是

方法Show没有重载接受0参数


DataSource
属性分配需要
IEnumerable
作为其源。您需要更改
Show
方法以返回实现
IEnumerable
(例如
List
)的任何对象,并使用
id
参数调用它,如下所示:

// Get list object
public List<GradeSheetViewModel> Show(int id)
{
    return _repGrade.GetList(id);
}

// Controller
public ActionResult ExportToExcel(int id)
{
    var gv = new GridView();
    gv.DataSource = Show(id);

    // other stuff
}

附加说明2:避免使用类似MVC控制器的webforms服务器控件。您可以从中选择一个可用选项。

方法Show有一个参数id,您可以不使用参数调用它。我如何使用函数中的参数调用它@Jean-ClaudeColettePlease阅读-总结是,这不是一种向志愿者致辞的理想方式,可能会对获得答案产生反作用。请不要将此添加到您的问题中。
// Get list object
public List<GradeSheetViewModel> Show(int id)
{
    return _repGrade.GetList(id);
}

// Controller
public ActionResult ExportToExcel(int id)
{
    var gv = new GridView();
    gv.DataSource = Show(id);

    // other stuff
}
public ActionResult ExportToExcel(int id)
{
    var gv = new GridView();
    gv.DataSource = Show(id);
    gv.DataBind();

    // save the file and create byte array from stream here

    byte[] byteArrayOfFile = stream.ToArray();

    return File(byteArrayOfFile, "application/vnd.ms-excel", "DemoExcel.xls");
}