Action Filter to get AddProductToCart_Details action and return a view

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
8 years ago
Hi guys i have been trying to create a filter when the user add a item to cart and the quantity is greater than the stock, i want to return a custom view so the user can choice what they want to do next, but i am unable to show the view, this is my code (everything is ok the filter is working but i am unable to return the view):


        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            var _productService = EngineContext.Current.Resolve<IProductService>();
            FormCollection form = (FormCollection)filterContext.ActionParameters["form"];
            int productId = (int)filterContext.ActionParameters["productId"];
            var product = _productService.GetProductById(productId);


            int quantity = 1;
            foreach (string formKey in form.AllKeys)
                if (formKey.Equals(string.Format("addtocart_{0}.EnteredQuantity", productId),     StringComparison.InvariantCultureIgnoreCase))
                {
                    int.TryParse(form[formKey], out quantity);
                    break;
                }


            if (product.StockQuantity < quantity)
            {

                var subscribeUrl = new UrlHelper(filterContext.RequestContext).RouteUrl("Plugin.Triquimas.CheckOut.OutOfStock");
                filterContext.Result = new RedirectResult(subscribeUrl);

            }

What im doing wrong, In advance thanks for your help.
8 years ago
I think you should redirect customer to another page in that case. Below sample working action filter you have to modify.

return filterContext;


should return it.

Here is an working sample, hope this will help.



public ActionExecutingContext runActionFilter(ActionExecutingContext filterContext)
        {

            string actionName = filterContext.ActionDescriptor.ActionName;

            if (actionName == "ProductDetails" || actionName == "AddProductToCart_Details" )
            {


                HttpRequestBase request = filterContext.HttpContext.Request;
                string controllerName = filterContext.Controller.ToString();


                var storeInformationSettings = EngineContext.Current.Resolve<StoreInformationSettings>();

                var username = filterContext.Controller.ControllerContext.HttpContext.User.Identity.Name.ToString();

                int productId = 0;
                foreach (var key in filterContext.ActionParameters)
                {
                    if (key.Key == "productId")
                    {
                        productId = Convert.ToInt32(key.Value);
                    }
                    

                }

                var productService = EngineContext.Current.Resolve<IProductService>();
                var product = productService.GetProductById(productId);
                product.Name = "test -- " + product.Name;
                product.CustomerEntersPrice = false;
               // product.Price = product.Price;

                log("_Modified_" + controllerName, username + " - "+product.Name );


                filterContext.Controller.ViewData.Model = product;

                
            }

            if (actionName == "Cart")
            {

                HttpRequestBase request = filterContext.HttpContext.Request;
                string controllerName = filterContext.Controller.ToString();
            }


            return filterContext;

        }


        public  string RenderPartialViewToString(ControllerContext controllerContext, string viewName, object model)
        {
            controllerContext.Controller.ViewData.Model = model;
            try
            {
                using (StringWriter sw = new StringWriter())
                {
                    ViewEngineResult viewResult = System.Web.Mvc.ViewEngines.Engines.FindPartialView(controllerContext, viewName);
                    ViewContext viewContext = new ViewContext(controllerContext, viewResult.View, controllerContext.Controller.ViewData, controllerContext.Controller.TempData, sw);
                    viewResult.View.Render(viewContext, sw);

                    log("View_"+viewName+"_"+controllerContext.ToString(), sw.GetStringBuilder().ToString());
                    return sw.GetStringBuilder().ToString();
                }
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.