【问题标题】:'System.Web.Helpers.WebGrid' does not contain a definition for 'RenderControl'“System.Web.Helpers.WebGrid”不包含“RenderControl”的定义
【发布时间】:2014-01-22 07:13:22
【问题描述】:

我是在 asp.net 中使用 MVC 3 和 linq to SQL 的新手,我正在尝试将我的 webgrid 数据导出到 excel 工作表,但它会引发错误:

'System.Web.Helpers.WebGrid' does not contain a definition for 'RenderControl' and no extension method 'RenderControl' accepting a first argument of type 'System.Web.Helpers.WebGrid' could be found (are you missing a using directive or an assembly reference?) 

我正在发布代码,所以你们可以帮助我。 代码:

控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Helpers;
using EmployeeAttendance_app.Models;
using System.IO;
using System.Web.UI.WebControls;
using System.Web.Mvc.Html;


namespace EmployeeAttendance_app.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Precise Technology Consultants";
            var DataContext = new EmployeeAtdDataContext();
            //var EmployeeAtd = DataContext.GetAttendance_Sp();
            IEnumerable<GetAtdRecord_SpResult> EmployeeAtd = DataContext.GetAtdRecord_Sp(null).ToList();
            return View(EmployeeAtd);

        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult ViewData() 
        {
            return View();
        }

        public ActionResult ToExcel()
        {
            var DataContext = new EmployeeAtdDataContext(); 

            IEnumerable<GetAtdRecord_SpResult> EmployeeAtd = DataContext.GetAtdRecord_Sp(null).ToList();

            var grid = new WebGrid(EmployeeAtd, defaultSort: "EmplID");
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=AttendanceSheet.xls");
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
            grid.RenderControl(htw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
            return RedirectToAction("Index");
        }
    }
}

这里是应该显示数据的视图,然后单击导出应该导出文件 查看:

@using EmployeeAttendance_app.Models



<div>

@{

    var grid = new WebGrid(ViewData.Model, defaultSort: "EmplID");

}

@grid.GetHtml()

 @using (Html.BeginForm("ToExcel","Home",FormMethod.Post))
{
    <button type="submit" >To Excel</button>
}
</div>

【问题讨论】:

    标签: asp.net asp.net-mvc c#-4.0 linq-to-sql export-to-excel


    【解决方案1】:

    你应该为你的数据命名参数:

    var grid = new WebGrid(source: ViewData.Model, defaultSort: "EmplID");
    

    此外,源必须是IEnumerable&lt;object&gt; 类型,因此如果您的模型类没有实现IEnumerable&lt;object&gt;,您应该更改它或指定模型的属性以用作WebGrid 助手的源数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多