Developer forum

Forum » Development » How Are CustomOrderViewModel Properties Populated in Templates?

How Are CustomOrderViewModel Properties Populated in Templates?

Pedro Meias
Reply

Hi,

Working on DW10

We are trying to extend the OrderViewModel.

The goal is to expose order-related documents on the frontend template.

What we've done so far:

  • Created a CustomOrderViewModel that extends OrderViewModel

  • Added properties like List<Document> Invoices, List<Document> ShippingDocuments, and List<Document> OtherAttachments

  • Created a Document class with:

    • Name

    • Type

    • CreatedDate / LastUpdatedDate

    • DownloadUrl (with the download attribute for browser download)

My main question is:
How are the properties I define in CustomOrderViewModel actually populated?

Sharing my code

Document Class

namespace Dna.Kent.Orders.Entities;

public class Document
{
    public string Name { get; set; }
    public string Type { get; set; }
    public DateTime CreatedDate { get; set; }
    public string DownloadUrl { get; set; }

    public Document()
    {
        //Default Constructor
    }

    public Document(string name, string type, DateTime createdDate, string downloadUrl)
    {
        Name = name;
        Type = type;
        CreatedDate = createdDate;
        DownloadUrl = downloadUrl;
    }
}

CustomOrderViewModel

 

using Dynamicweb.Ecommerce.Frontend;

namespace Dna.Kent.Orders.Entities;

public class CustomOrderViewModel : OrderViewModel
{
    public List<Document> shippingDocuments { get; set; }
    public List<Document> Invoices { get; set; }
    public List<Document> AdditionalAttachments { get; set; }

    public CustomOrderViewModel()
    {
        //Default Constructor
    }
}

 


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Pedro

You would have to do that your self - e.g. in the contructor and you can use DI to require the needed services.

But this might not be the best approach to do what you want.

Simply create a number of extension methods to return what you need:

public static class OrderViewModelExtensions{
    public static List<Document> GetShippingDocuments(this OrderViewModel orderViewModel)
    {
        return [];
    }
}

The only situation where you want to use custom viewmodels is if you want to return the data along with the orderviewmodel as part of a webapi call to /dwapi. And usually it would be a better solution to create a seperate endpoint for e.g. ShippingDocuments in this case to avoid performance issues.

Votes for this answer: 1
 
Pedro Meias
Reply

Hi Nicolai.

Thank you. We will go with the extension methods.

 

You must be logged in to post in the forum