我正在尝试通过MVC操作过滤器修改添加到购物车的产品数量。这就是我所拥有的,但它不起作用:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var settings = _settingService.LoadSetting<RRGamesBaseSettings>();
var customer = _workContext.CurrentCustomer;
if (customer.IsInCustomerRole(settings.ResellerRoleSystemName))
{
var productId = Convert.ToInt32(filterContext.ActionParameters["productId"]);
var formCollection = new FormCollection(filterContext.Controller.ControllerContext.HttpContext.Request.Form);
var productService = EngineContext.Current.Resolve<IRRGamesProductService>();
var product = productService.GetProductById(productId);
int quantity = 0;
string fieldName = string.Format("addtocart_{0}.EnteredQuantity", productId);
int.TryParse(formCollection[fieldName], out quantity);
if (product != null)
{
if (quantity < product.CaseQuantity)
{
quantity = product.CaseQuantity;
formCollection[fieldName] = quantity.ToString();
}
}
}
base.OnActionExecuting(filterContext);
}
当我将formCollection [formKey]设置为新数量时,它不会在转到控制器操作时更新表单值。
答案 0 :(得分:3)
我想通了,很简单
formCollection.Set(fieldName, quantity.ToString());
filterContext.ActionParameters["form"] = formCollection;