[英]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
中我有不同的数据类型( int
、 string
、 double
)。
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.