I'm trying to Implement ExportToPDF()
action method in my MCV.CORE
web app.
Now obviously in my EmployeeCategory class
I have different data types(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[]
No best type found for implicitly-typed array
My simplified 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");
}
}
CodePudding user response:
I don't think this error has anything to do with the data types of your Employee
properties and has more to do with you trying to cast an Employee
to a string[]
:
string[] employee = (string[])employees[i];
Aside from this not being possible (using the method above) I don't think it is necessary as you can obtain the properties and their values to output them as it appears you're attempting to do in your question.
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>");
});