Developer forum

Forum » Development » ProductViewModel - How to manipulate values after creation?

ProductViewModel - How to manipulate values after creation?

Christian Fisker
Reply

Hi DW,

Is there a way to change the values in a ProductViewModel after it has been created in the ViewModelFactory?

The only way I have found sofar is to do the modification in f.ex. the ProductViewDetail.cshtml or ProductViewList.cshtml templates (swift/ecom/productcatalog)

I am specifically trying to modify the DefaultImage.Value property so that I can insert my own ImageURL to be used by my CustomImageHandler HttpHandler, which intercepts the standard GetImage.ashx, to get product images from an external source.

Thanks,
  Christian


Replies

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

You can extend the viewmodels by inheriting them.

But please be careful and do not do stuff that will be slow as these models are created many times.

This will extend the viewmodel in the template and also in the webapi.

public class CustomProductViewmodel : ProductViewModel
    {
        public CustomProductViewmodel()
        {
            //This will not work as the header will be overriden later in the initialisation process of this object.
            base.Name = "Set name in constructor does not work";
        }

        /// <summary>
        /// Take over default property of the base viewmodel. Suitable to change default behavior
        /// </summary>
        public new string Name
        {
            get
            {
                return $"{base.Name} changed";
            }
        }

        /// <summary>
        /// Addtional data property available in template. Suitable for additional data
        /// </summary>
        public string NameOrTile
        {
            get
            {
                if (!string.IsNullOrEmpty(base.MetaTitle))
                {
                    return MetaTitle;
                }
                else
                {
                    return Name;
                }
            }
        }

        /// <summary>
        /// Method to call from template using data from the model instance. Suitable for rendering logic
        /// </summary>
        /// <returns></returns>
        public string RenderedPrice()
        {
            return $"<small>{Price.PriceFormatted}</small>";
        }
    }
Votes for this answer: 1
 
Christian Fisker
Reply

Thanks Nicolai,

I suppose the magic happens here:

/Christian

 

 
Martin Moen
Reply

I tried this, but got this error:

'ProductViewModel' does not contain a definition for 'NameOrTitle'

Are there anything else we need to do in order for it to work?

This was tested in a template that inherits ParagraphViewModel like this:

@inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.ParagraphViewModel>

ProductViewModel is fetched like this:

ProductViewModel product = null;
if (Dynamicweb.Context.Current.Items.Contains("ProductDetails"))
{
  product = (ProductViewModel)Dynamicweb.Context.Current.Items["ProductDetails"];
}
 
And product.NameOrTitle throws the error.
 
This is the extension code:
What am I missing?!
 
Christian Fisker
Reply

Hi Martin,

You must typecast to your own type.

So this

ProductViewModel product = null;
if (Dynamicweb.Context.Current.Items.Contains("ProductDetails"))
{
  product = (ProductViewModel)Dynamicweb.Context.Current.Items["ProductDetails"];
}

Turns into this

CustomProductViewModel product = null;
if (Dynamicweb.Context.Current.Items.Contains("ProductDetails"))
{
  product = (CustomProductViewModel)Dynamicweb.Context.Current.Items["ProductDetails"];
}

 

You must be logged in to post in the forum