Skip to content

ViewModel Binding issue #74

@ivoryguard

Description

@ivoryguard

Hello.
I found an issue binding ViewModel.

It works when assigning ViewModel data to DocumentFactory.Create

var data = New MyDataViewModel();
var document = DocumentFactory.Create("TestModel.cs.docx", data);
document.Generate("Output.docx");

However, assigning ViewModel data to DocumentBase.Generate method makes a compile error;

var data = New MyDataViewModel();
var document = DocumentFactory.Create("TestModel.cs.docx");
document.Generate("Output.docx", data);

The compile error message is as the followings;

SharpDocx.SharpDocxCompilationException was unhandled
  Errors=Line 0: warning CS1685: The predefined type 'System.ObsoleteAttribute' is defined in multiple assemblies with the global alias. Using definition from 'c:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll'.

Line 16: error CS1061: 'string' does not contain a definition for 'Title' and no extension method 'Title' accepting a first argument of type 'string' could be found. Are you missing a using directive or an assembly reference?
...

I can confirm the generated source code as the followings;
You can find "public string Model { get; set; }" line.
It must be "public MyDataViewModel Model { get; set; }"

SourceCode=    1  using System;
    2  using DocumentFormat.OpenXml.Wordprocessing;
    3  using SharpDocx.CodeBlocks;
    4  using SharpDocx.Models;
    5  
    6  
    7  namespace SharpDocx
    8  {
    9      public class SharpDocument_fe17ba9fef8642ff96f06766fd998ad8 : DocumentBase
   10      {
   11          public string Model { get; set; }
   12  
   13          protected override void InvokeDocumentCode()
   14          {
   15              CurrentCodeBlock = CodeBlocks[0];
   16              Write(Model.Title);
   17              CurrentCodeBlock = CodeBlocks[1];
   18              Write(Model.Description);
   19              CurrentTextBlockStack.Push(CurrentTextBlock);
   20              CurrentTextBlock = CodeBlocks[2] as TextBlock;
   21              CurrentCodeBlock = CodeBlocks[2];
...

I tested with .NET Framework 4.8 console project and .NET 6 console project but the error was same.

Best regards;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions