nopcommerce从操作中的批量产品编辑窗体中获取值



好的,所以我有操作过滤器工作,但我不确定如何从批量编辑表单中提取值。

我已经在依赖项注册器中注册了过滤器。

这是我的过滤器:

using Nop.Admin.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace Nop.Plugin.Feed.Froogle.Actions
{
    public class BulkEditOverideActionFilter : ActionFilterAttribute, IFilterProvider
    {
        public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
        {
            if (controllerContext.Controller is ProductController && actionDescriptor.ActionName.Equals("BulkEditUpdate", StringComparison.InvariantCultureIgnoreCase))
            {
                return new List<Filter>() { new Filter(this, FilterScope.Action, 0) };
            }
            return new List<Filter>();
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            System.Diagnostics.Debug.WriteLine("The filter action is = " + filterContext.ActionDescriptor.ActionName.ToString());
            if (filterContext != null)// && amazonListingActive == true
            {
                var form = (FormCollection)filterContext.ActionParameters.FirstOrDefault(x => x.Key == "form").Value;
                foreach (var i in form.Keys)
                {
                    System.Diagnostics.Debug.WriteLine("The key value is = " + i);
                }
            }
        }
    }

任何人都知道我如何适应这一点。

好的,我想通了。表单返回一个 namevaluecollection,因此必须使用它来获取值。

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
        var httpContext = HttpContext.Current;
        if (httpContext != null && httpContext.Request.Form.HasKeys())
        {
            var form = filterContext.HttpContext.Request.Form;
            var products = form.AllKeys.SelectMany(form.GetValues, (k, v) => new { key = k, value = v });
            System.Diagnostics.Debug.WriteLine("The form is = " + form);
            if (form != null)
            {
                foreach (var pModel in products)
                {
                    System.Diagnostics.Debug.WriteLine("The pModel.Key is = " + pModel.key);
                    System.Diagnostics.Debug.WriteLine("The pModel.Value is = " + pModel.value);
                }
            }
        }
}

更新这就是为什么我这样做并且无法使用申请表。

有一个产品保存事件,我用它来将信息保存在其他事件中 我创建的产品编辑选项卡。在那里,我可以像这样使用申请表。

public class ProductSaveConsumer : IConsumer<EntityFinalised<Product>>
{
    public void HandleEvent(EntityFinalised<Product> eventMessage)
    {
        var httpContext = HttpContext.Current;
        if (httpContext != null) {
            amazonProduct = new AmazonProduct();
            var form = httpContext.Request.Form;
            //now I can grab my custom form values
            if (!String.IsNullOrEmpty(form["AmazonProductSKU"]))
                amazonProduct.AmazonProductSKU = form["AmazonProductSKU"].ToString();
        }
    }
}

但是批量编辑过滤器返回一个 NameValueCollection,所以我不能 使用Request.Form

希望这对其他人有所帮助:)

最新更新