nopcommerce 在操作中从批量产品编辑表单中获取值

nopcommerce Getting the values from the bulk product edit form in the action

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

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

这是我的过滤器:

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);
                }
            }
        }
    }

任何人都知道我如何完成这个。

好的,我明白了。 表单 returns 是一个名称值集合,因此必须使用它来获取值。

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);
                }
            }
        }
}

UPDATE This is why I did it this way and couldn't use request form.

I have a product save event, which I use to save info in a additional product edit tab I created. In there I can use request form like so.

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();
        }
    }
}

But the bulk edit filter returns a NameValueCollection, So I couldn't use Request.Form

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