.NET Core 自定义模型绑定器调用默认模型绑定器
Posted
技术标签:
【中文标题】.NET Core 自定义模型绑定器调用默认模型绑定器【英文标题】:.NET Core Custom model binder call default model binder 【发布时间】:2018-08-22 09:55:43 【问题描述】:我正在尝试在 .NET Core 中实现一些将小数四舍五入到小数点后两位的中间件。所有其他映射都可以像现在通过ComplexTypeModelBinder
那样工作。我曾尝试在我之前调用该绑定器或从它继承,但最终模型在碰到控制器时只是为空。
基本上我追求的功能与此处询问的功能相同:Call Default Model Binder from a Custom Model Binder?,但适用于 .NET 核心。
【问题讨论】:
请贴一些代码 将其作为第一个模型活页夹插入ModelBinders.Binders.Insert(typeof(Decimal), new DateTimeModelBinder(), 0);
我认为正确的答案,或接近它,将是使用 DI 并注入您需要的众多粘合剂之一:github.com/aspnet/AspNetCore/tree/master/src/Mvc/Mvc.Core/src/…
【参考方案1】:
public class JqGridModelBinder : IModelBinder
public Task BindModelAsync(ModelBindingContext bindingContext)
if (bindingContext == null)
throw new ArgumentNullException(nameof(bindingContext));
var request = bindingContext?.ActionContext?.HttpContext?.Request;
var param = new JqGridParam
isSearch = bool.Parse(request.Query["_search"]),
pageIndex = int.Parse(request.Query["page"]),
pageSize = int.Parse(request.Query["rows"]),
sortColumn = request.Query["sidx"].ToString(),
sortOrder = request.Query["sord"].ToString(),
id = request.Query["id"].ToString(),
param = request.Query["oper"].ToString(),
editOper = request.Query["edit"].ToString(),
addOper = request.Query["add"].ToString(),
delOper = request.Query["del"].ToString(),
@where = JqGridFilter.Create(request.Query["filters"]),
//operation = (OPERATION)System.Enum.Parse(typeof(OPERATION), request.Query["oper"]=="null" ? "none": request.Query["oper"].ToString())
;
bindingContext.Result = ModelBindingResult.Success(param);
return Task.CompletedTask;
【讨论】:
为您的代码写一些描述并正确格式化代码。【参考方案2】:public enum OPERATION
none,
add,
del,
edit,
excel
[ModelBinder(BinderType = typeof(JqGridModelBinder))]
public class JqGridParam
[BindRequired]
public int pageIndex get; set;
[BindRequired]
public int pageSize get; set;
[BindRequired]
public string sortColumn get; set;
[BindRequired]
public string sortOrder get; set;
[BindRequired]
public bool isSearch get; set;
[BindRequired]
public string id get; set;
[BindRequired]
public string param get; set;
[BindRequired]
public string editOper get; set;
[BindRequired]
public string addOper get; set;
[BindRequired]
public string delOper get; set;
[BindRequired]
public JqGridFilter where get; set;
[BindRequired]
public OPERATION operation get; set;
[DataContract]
public class JqGridFilter
[DataMember]
public string groupOp get; set;
[DataMember]
public Rule[] rules get; set;
public static JqGridFilter Create(string jsonData)
try
var serializer = new DataContractJsonSerializer(typeof(JqGridFilter));
//System.IO.StringReader reader = new System.IO.StringReader(jsonData);
System.IO.MemoryStream ms = new System.IO.MemoryStream(Encoding.Default.GetBytes(jsonData));
return serializer.ReadObject(ms) as JqGridFilter;
catch
return null;
[DataContract]
public class Rule
[DataMember]
public string field get; set;
[DataMember]
public string op get; set;
[DataMember]
public string data get; set;
【讨论】:
以上是关于.NET Core 自定义模型绑定器调用默认模型绑定器的主要内容,如果未能解决你的问题,请参考以下文章
Asp.Net Core Razor Page PUT Handler 模型绑定器未绑定来自 ajax put 上的 java 脚本的序列化表单