SharpDocx - foreach statement shows only last element of list

1k Views Asked by At

I'm using SharpDocx to generate a docx file from a template, using my custom model. Everything works fine except the foreach statement that returns only the last element of my list regardless i got a few more.

Model:

public class ReportDocxModel
{
    public ReportDocxModel()
    {
        Contatti = new List<Contatti>();
    }

    public List<Contatti> Contatti { get; set; }
}

Method to generate the document:

var document = DocumentFactory.Create(filePath, model);
document.Generate(Path.Combine(rootPath, tempFileName));

Segment inside the template (extension cs.docx) where i loop my list enter image description here

I followed the example and the demo here but it doesn't work. I'm on Asp.NET Core 3.1 and SharpDocx version is 1.2.1 (latest)

Any help would be appreciated, thanks

1

There are 1 best solutions below

2
egonl On BEST ANSWER

You didn't quite follow the example, since you forgot <% AppendRow(); %> after <%= c.Email %>. If you don't call AppendRow(), no rows will be appended to the table. That's why you only see a table with one row, containing the last element.