简体   繁体   English

我在这个实现中做错了什么?

[英]What I'm doing wrong with this implementation?

I'm trying to Implement ExportToPDF() action method in my MCV.CORE web app.我正在尝试在我的MCV.CORE网络应用程序中实施ExportToPDF()操作方法。 Now obviously in my EmployeeCategory class I have different data types( int , string , double ).现在显然在我的EmployeeCategory class中我有不同的数据类型( intstringdouble )。

So, my conclusion why I'm getting error, is because data type needs to be only string type.所以,我得出错误的结论是因为数据类型只能是字符串类型。 Which is not possible in my case.在我的情况下这是不可能的。

I'm not sure how to correctly implement this action method.我不确定如何正确实施此操作方法。

Errors that I'm getting is:我得到的错误是:

Cannot convert type Test_Project_Web.Models.EmployeeCategory to string[]无法将类型Test_Project_Web.Models.EmployeeCategory to string[]

No best type found for implicitly-typed array

My simplified code:我的简化code:

EmployeeCategory.cs :
public class EmployeeCategory
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string? Name { get; set; }

    [Required]
    public string? LastName { get; set; }

    [Required]
    public string? Address { get; set; }

    [Required]
    public double NetSalary { get; set; }
    
    [Required]
    public double GrossSalary { get; set; }



}
EmployeeCategoryController.cs :

        [HttpPost]
        public FileResult ExportToPDF()
        {
            List<EmployeeCategory> employees = (from employee in Context.EmployeeCategories.Take(9)
                                                select new[] {

                                      employee.Id,
                                      employee.Name,
                                      employee.LastName,
                                      employee.Address,                                     
                                      employee.NetSalary,
                                      employee.GrossSalary
                                 }).ToList<EmployeeCategory>();

            

            //Building an HTML string.
            StringBuilder sb = new StringBuilder();

            //Table start.
            sb.Append("<table border='1' cellpadding='5' cellspacing='0' style='border: 1px solid #ccc;font-family: Arial; font-size: 10pt;'>");

            //Building the Header row.
            sb.Append("<tr>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>Id</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>Name</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>LastName</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>Address</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>NetSalary</th>");
            sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>GrossSalary</th>");
            sb.Append("</tr>");

            //Building the Data rows.
            for (int i = 0; i < employees.Count; i++)
            {
                string[] employee = (string[])employees[i];
                sb.Append("<tr>");
                for (int j = 0; j < employee.Length; j++)
                {
                    //Append data.
                    sb.Append("<td style='border: 1px solid #ccc'>");
                    sb.Append(employee[j]);
                    sb.Append("</td>");
                }
                sb.Append("</tr>");
            }



            //Table end.
            sb.Append("</table>");

            using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(sb.ToString())))
            {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                PdfWriter writer = new PdfWriter(byteArrayOutputStream);
                PdfDocument pdfDocument = new PdfDocument(writer);
                pdfDocument.SetDefaultPageSize(PageSize.A4);
                HtmlConverter.ConvertToPdf(stream, pdfDocument);
                pdfDocument.Close();
                return File(byteArrayOutputStream.ToArray(), "application/pdf", "EmployeeList.pdf");
            }
        }

Context.EmployeeCategories does not contain arrays of EmployeeCategory , it just contains EmployeeCategory s. Context.EmployeeCategories不包含EmployeeCategory的数组,它只包含EmployeeCategory s。 Remove the array initializer:删除数组初始值设定项:

  List<EmployeeCategory> employees = (from employee in Context.EmployeeCategories.Take(9)
    select new {
      employee.Id,
      employee.Name,
      employee.LastName,
      employee.Address,                                     
      employee.NetSalary,
      employee.GrossSalary
 }).ToList<EmployeeCategory>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 这个log4net实现我做错了什么? - What am I doing wrong with this log4net implementation? Python和C#密码学:我做错了什么? - Python and C# Cryptography: What i'm doing wrong? 将字符串绑定到TextBlock WPF-我在做什么错? - Binding string to a TextBlock WPF - What I'm doing wrong? TypeDescriptor CanConvertFrom Bug? 还是我做错了? - TypeDescriptor CanConvertFrom Bug? or i'm doing it wrong? 我收到一个错误:使用未分配的局部变量“艺术家”,不知道我在做什么错? - I get an error: use of unassigned local variable 'artist', don't know what i'm doing wrong? 我无法弄清楚这个基本的NHibernate查询在做什么错 - I cannot figure out what I'm doing wrong with this basic NHibernate query 我在重用EF 5包含项时遇到问题。我在做什么错? - I'm having an issue reusing includes for EF 5. What am I doing wrong? 无法弄清楚我做错了什么 ASP.NET C# - Can't figure out what I'm doing wrong ASP.NET C# 不确定尝试创建/添加此命令时我做错了什么: - Unsure what I'm doing wrong when trying to create / add this command: DataContract运行时错误-类型&#39;myType&#39;无法序列化。 我做错了什么? - DataContract runtime error - Type 'myType' cannot be serialized. What i'm doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM