PrepareProductDetailsModel in CustomFilter OnResultExecuting

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
Il y a 3 ans
Why does the PrepareProductDetailsModel function not overwrite the model while if I change the name it works?

  public override void OnResultExecuting(ResultExecutingContext context)
        {

            if (!(context.ActionDescriptor is ControllerActionDescriptor actionDescriptor))
                return;

            if (actionDescriptor.ControllerTypeInfo != typeof(ProductController) ||
                actionDescriptor.ActionName != "ProductDetails" ||
                context.HttpContext.Request.Method != "GET")
            {
                return;
            }
            var result = context.Result as ViewResult;
            if (result == null)
                return;
            var model = result.Model as ProductDetailsModel;
            if (model == null)
                return;

            var product = _productService.GetProductById(model.Id);

            var attributesXml = ...................................;

            model = _productModelFactory.PrepareProductDetailsModel(product, new ShoppingCartItem
            {
                AttributesXml = attributesXml,
            }, false);
}


model.Name = "Test";
Il y a 3 ans
Hello havana7
Refer this link,i hope you will get your solutions.
Il y a 3 ans
Hello havana7
Refer below link.
I hope you will get your solutions.
https://www.majako.net/how-to-create-a-custom-action-filter-in-nopcommerce-v4
Il y a 3 ans
The problem is that updating the model does not work with the PrepareProductDetailsModel function
 model = _productModelFactory.PrepareProductDetailsModel(product, new ShoppingCartItem
            {
                AttributesXml = attributesXml,
            }, false);


while if I write
model.Name = "Test"; 

the model is updated.
Why?
Il y a 3 ans
Because your local model variable is a reference to the result.Model.  I.e.  model.Name affects the referenced object, whereas when you reassign model, you are only affecting your local variable.
Try
   result.Model =  _productModelFactory.PrepareProductDetailsModel(...

(I don't recall if it's readonly.  If it is, then you will need to replace each of the properties just like you did with Name)
Il y a 3 ans
Yes, unfortunately it is read only

Thanks
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.