问题
I'm trying to implement some middleware in .NET Core that rounds decimals to 2 decimal places. All of the other mapping can work as it currently does via the ComplexTypeModelBinder
. I've tried calling that binder before mine or inheriting from it, but it ends up with the model just being null when it hits the controller.
Essentially I'm after the same functionality as asked here: Call Default Model Binder from a Custom Model Binder?, but for .NET core.
回答1:
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; }
}
回答2:
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;
}
}
来源:https://stackoverflow.com/questions/49268105/net-core-custom-model-binder-call-default-model-binder