1
我想通过一个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]新的数量,它不会当它到控制器动作更新表单值。
不清楚是什么你要怎样做。你初始化一个'新的FormCollection'并对其进行修改(而不是'Request.Fo rm')。在任何情况下,本应在自做'ModelBinder' –
这个动作过滤器拦截的添加到购物车方法和改变形式的数量值。我需要使用动作过滤器,因为我在插件中这样做,所以我不修改应用程序的原始源代码。 – David
你不改变'Request.Form'价值 - 你改变它在它的一个副本,你取得了(你的变量'formCollection') –