Implementing the view model class

Because the web services in Sitefinity follow the MVVM pattern, you must implement a view model class by performing the following:

  1. From the context menu of the folder Web » Services » Data in the ProductsModule project, click Add » Class.
  2. Name the class file ProductItemViewModel.cs and click Add.
  3. Open the file ProductItemViewModel.cs.
  4. Add the following using statements:

    using ProductsModule.Model;
    using Telerik.Sitefinity.GenericContent.Model;
    using Telerik.Sitefinity.Modules;
    using Telerik.Sitefinity.Modules.GenericContent;

  5. Make the ProductItemViewModel class inherit the ContentViewModelBase class:

    public class ProductItemViewModel : ContentViewModelBase
    {
    }

  6. Create the following constructors:

    public ProductItemViewModel()
        : base()
    {
    }
     
    public ProductItemViewModel(ProductItem contentItem, ContentDataProviderBase provider)
        : base(contentItem, provider)
    {
        this.Price = contentItem.Price;
        this.QuantityInStock = contentItem.QuantityInStock;
        this.WhatIsInTheBox = contentItem.WhatIsInTheBox;
    }

  7. Define properties that match the properties in the ProductItem class. Following is the code for the Price, QuantityInStock, and WhatIsInTheBoxproperties:

    public decimal Price { get; set; }
     
    public int QuantityInStock { get; set; }
     
    public string WhatIsInTheBox { get; set; }

  8. Implement the abstract members of the ContentViewModelBase class in the following way:

    protected override Content GetLive()
    {
        return this.provider.GetLiveBase<ProductItem>((ProductItem)this.ContentItem);
    }

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK