我有一个场景,我有一个基类,我们称之为“PagingCriteriaBase”
public class PagingCriteriaBase : CriteriaBase
{
public Int32 CountOfItemsPerPage { get; set; }
public SortOrder SortingOrder { get; set; }
public String SortBy { get; set; }
public Int32 PageNo { get; set; }
public PagingCriteriaBase(Int32 pageNo,Int32 countOfItemsPerPage, SortOrder sortingOrder, String sortBy,Int32 draw)
{
this.PageNo = pageNo>0?pageNo:1;
this.CountOfItemsPerPage = countOfItemsPerPage>0?countOfItemsPerPage:10;
this.SortBy = sortBy;
this.SortingOrder = sortingOrder;
this.Draw = draw;
}
}
然后我有其他类将继承自“PagingCriteriaBase”,例如
public class UserCriteria:PagingCriteriaBase
{
public String Email { get; set; }
public String DisplayName { get; set; }
public UserCriteria():base(1,0,SortOrder.Asc,"",1)
{
}
public UserCriteria(Int32 pageNo,Int32 countOfItemsPerPage, SortOrder sortingOrder, String sortBy, Int32 draw)
:base(pageNo, countOfItemsPerPage,sortingOrder,sortBy,draw)
{
}
}
现在我想做的是我想要创建一个将与Web API方法一起使用的Model Binder,并且模型绑定器将与“PagingCriteriaBase”的所有子类一起使用,这个模型的目的binder是根据来自ajax请求的数据设置一些属性,我尝试执行以下操作:
我创建了一个实现“IModelBinder”的类,如下所示:
public class PagingModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (!bindingContext.ModelType.IsSubclassOf(typeof(PagingCriteriaBase)))
{
return Task.FromResult(false);
}
String startModelName = "start";
String lengthModelName = "length";
var startResult = bindingContext.ValueProvider.GetValue(startModelName);
var lengthResult = bindingContext.ValueProvider.GetValue(lengthModelName);
Int32 start, length;
if (!Int32.TryParse(startResult.FirstValue, out start))
{
start = 0;
}
if (!Int32.TryParse(lengthResult.FirstValue, out length))
{
length = SystemProp.PAGE_SIZE;
}
else
{
length = 20;
}
var model = Activator.CreateInstance(bindingContext.ModelType);
Int32 pageNo = (int)Math.Ceiling((decimal)start / length);
bindingContext.ModelState.SetModelValue("PageNo", new ValueProviderResult(pageNo.ToString()));
bindingContext.ModelState.SetModelValue("CountOfItemsPerPage", new ValueProviderResult(length.ToString()));
bindingContext.Model = model;
var mProv = (IModelMetadataProvider)bindingContext.HttpContext.RequestServices.GetService(typeof(IModelMetadataProvider));
bindingContext.Result = ModelBindingResult.Success(model);
return Task.CompletedTask;
}
}
我创建了一个ModelBinderProvider,如下所示:
public class PagingEntityBinderProvider:IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (context.Metadata.ModelType == typeof(PagingCriteriaBase))
{
return new BinderTypeModelBinder(typeof(PagingModelBinder));
}
return null;
}
}
我使用以下方式注册了模型绑定器:
services.AddMvc(op => op.ModelBinderProviders.Insert(0, new PagingEntityBinderProvider())) ;
在我的Web API方法中,我执行了以下操作:
public IActionResult GetAll([ModelBinder(typeof(PagingModelBinder))]UserCriteria crit)
{
//Code goes here
}
当我使用上面的模型绑定器时,我发现一旦代码到达Web API方法,类中的值就不会改变,例如“PageNo”属性保持为1,所以我需要做的是让模型绑定器设置子类对象的所有相关属性,而不管类本身的类型如何,一旦代码到达Web API方法,模型将正确设置所有属性,请指点我我需要在代码中更改哪些内容来处理此问题?
请注意我使用的是Asp.Net Core 2.0
答案 0 :(得分:0)
我想那是因为你没有设置模型的任何属性,只是实例化它。
我认为我们可以通过反射来调整子类的所有属性,并根据模型状态值设置值(假设属性名称与模型状态键相同)
public Task BindModelAsync(ModelBindingContext bindingContext)
{
...
bindingContext.ModelState.SetModelValue("PageNo", new ValueProviderResult(pageNo.ToString()));
bindingContext.ModelState.SetModelValue("CountOfItemsPerPage", new ValueProviderResult(length.ToString()));
ModelStateEntry v;
foreach (PropertyInfo pi in bindingContext.ModelType.GetProperties())
{
if (bindingContext.ModelState.TryGetValue(pi.Name, out v))
{
try
{
pi.SetValue(model, v.RawValue);
}
catch
{
}
}
}
bindingContext.Model = model;
...
}
并更改您的PagingEntityBinderProvider
public class PagingEntityBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (typeof(PagingCriteriaBase).IsAssignableFrom(context.Metadata.ModelType))
{
return new BinderTypeModelBinder(typeof(PagingModelBinder));
}
return null;
}
}
从Web API方法中删除ModelBinder属性
public IActionResult GetAll(UserCriteria crit)
{
//Code goes here
}