MVC 4 将模型从视图传递到泛型方法

MVC 4 Passing a model from a view to a generic method

我正在尝试在采用通用视图模型的各种 Intranet 报告页面上创建导出到 excel 按钮。我想从我的应用程序中的任何视图访问此方法,但现在我正在尝试从单个控制器访问它。 (注意:viewmodel必须从view传递过来,因为viewmodel是用户过滤的)

我的controller中的方法如下:

public void ExportToExcel<T>(T model, string fileName)
        {
            GridView gv = new GridView();
            var lst = (IEnumerable<T>)model;
            gv.DataSource = lst.ToList();
            gv.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".xls");
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            StringWriter sw = new StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
            gv.RenderControl(htw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
            Response.Clear();
        }

我使用以下方式调用它:

<a href="@Url.Action("ExportToExcel", new { model = (IEnumerable< Reporting.Models.HelplineSalesViewModel >) Model, fileName = "HelplineSales" })">Export to Excel</a>

点击时出现错误:

"Cannot call action method 'Void ExportToExcel[T](T, System.String)' on controller 'Reporting.Controllers.PostersController' because the action method is a generic method. Parameter name: methodInfo "

"Your error says that you can't have a generic Action method in your controller. What you'll need to do is set up separate typed Action methods for each type of object you want to export to Excel. Then call this generic method from each of them." – 磷虾