C# nopcommerce从操作中的批量产品编辑表单获取值
好的,我让动作过滤器工作,但我不确定如何从批量编辑表单中提取值 我已经在Dependence Registrator中注册了过滤器 这是我的过滤器:C# nopcommerce从操作中的批量产品编辑表单获取值,c#,forms,filter,action,nopcommerce,C#,Forms,Filter,Action,Nopcommerce,好的,我让动作过滤器工作,但我不确定如何从批量编辑表单中提取值 我已经在Dependence Registrator中注册了过滤器 这是我的过滤器: using Nop.Admin.Controllers; using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; namespace Nop.Plugin.Feed.Froogle.Actions { public c
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);
}
}
}
}
使用Nop.Admin.Controllers;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web.Mvc;
命名空间Nop.Plugin.Feed.Froogle.Actions
{
公共类BulkEditorIdeActionFilter:ActionFilterAttribute,IFilterProvider
{
公共IEnumerable GetFilters(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor)
{
if(controllerContext.Controller是ProductController&&actionDescriptor.ActionName.Equals(“BulkEditUpdate”,StringComparison.InvariantCultureIgnoreCase))
{
返回新列表(){new Filter(this,FilterScope.Action,0)};
}
返回新列表();
}
公共覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
{
System.Diagnostics.Debug.WriteLine(“过滤器操作是=“+filterContext.ActionDescriptor.ActionName.ToString());
如果(filterContext!=null)/&&amazonListingActive==true
{
var form=(FormCollection)filterContext.ActionParameters.FirstOrDefault(x=>x.Key==“form”).Value;
foreach(form.Keys中的变量i)
{
System.Diagnostics.Debug.WriteLine(“键值为=“+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();
}
}
}
公共类ProductSaveConsumer:IConsumer
{
public void HandleEvent(EntityFinalized事件消息)
{
var httpContext=httpContext.Current;
if(httpContext!=null){
amazonProduct=新的amazonProduct();
var form=httpContext.Request.form;
//现在我可以获取自定义表单值
如果(!String.IsNullOrEmpty(表单[“AmazonProductSKU”]))
amazonProduct.AmazonProductSKU=形式[“AmazonProductSKU”].ToString();
}
}
}
但是批量编辑筛选器返回NameValueCollection,因此我无法
使用Request.Form
希望这对其他人有所帮助:)你具体想要什么?@div查看我的答案了解我的意思,干杯。你不是刚提出请求。表单到keyvaluepair enumerable吗?@Raphael查看我的更新答案,我先尝试了:)