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:
- From the context menu of the folder Web » Services » Data in the ProductsModule project, click Add » Class.
- Name the class file ProductItemViewModel.cs and click Add.
- Open the file ProductItemViewModel.cs.
-
Add the following using statements:
using ProductsModule.Model;
using Telerik.Sitefinity.GenericContent.Model;
using Telerik.Sitefinity.Modules;
using Telerik.Sitefinity.Modules.GenericContent;
-
Make the ProductItemViewModel class inherit the ContentViewModelBase class:
public class ProductItemViewModel : ContentViewModelBase
{
}
-
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;
}
-
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; }
-
Implement the abstract members of the ContentViewModelBase class in the following way:
protected override Content GetLive()
{
return this.provider.GetLiveBase<ProductItem>((ProductItem)this.ContentItem);
}