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

c86crjj0  于 2022-12-20  发布在  .NET
关注(0)|答案(1)|浏览(147)

我尝试在我的MCV.CORE web应用中实现ExportToPDF()动作方法。现在很明显,在我的EmployeeCategory class中,我有不同的数据类型(intstringdouble)。
所以,我得出的结论是,为什么我会得到错误,是因为数据类型必须是字符串类型,这在我的例子中是不可能的。
我不知道如何正确地实现这个动作方法。
我得到的错误是:
无法转换类型Test_Project_Web.Models.EmployeeCategory to string[]
No best type found for implicitly-typed array
我的简化版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 :
private ApplicationDbContext Context { get; }
 
    public EmployeeCategoryController(ApplicationDbContext _context)
    {
        Context = _context;
    }
 
    public IActionResult Index()
    {
        return View(this.Context.EmployeeCategories.Take(6).ToList());
    }

        [HttpPost]
        public FileResult ExportToPDF()
        {
            List<EmployeeCategory> employees = (from employee in Context.EmployeeCategories.Take(6)
                                                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");
            }
        }
6jygbczu

6jygbczu1#

我不认为这个错误与Employee属性的数据类型有任何关系,而与您试图将Employee转换为string[]有更多关系:

string[] employee = (string[])employees[i];

除了这是不可能的(使用上面的方法),我不认为这是必要的,因为你可以获得属性和它们的值,以输出它们,因为它似乎你试图在你的问题。

employees.ForEach(employee =>
{
    sb.Append("<tr>");
    foreach (var propertyInfo in employee.GetType().GetProperties())
    {
        sb.Append("<td style='border: 1px solid #ccc'>");
        sb.Append(propertyInfo.GetValue(employee));
        sb.Append("</td>");
    }
    sb.Append("</tr>");
});

相关问题