173 lines
7.1 KiB
C#
173 lines
7.1 KiB
C#
//using System.ComponentModel;
|
|
//using System.Data;
|
|
//using System.Web.Mvc;
|
|
//using System.Collections.Generic;
|
|
//using System.Linq;
|
|
//namespace YLErp.Web.App_Start
|
|
//{
|
|
// public class TrimModelBinder : DefaultModelBinder
|
|
// {
|
|
// /// <summary>
|
|
// /// MVC模型绑定时去除字符串两端空格
|
|
// /// </summary>
|
|
// public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
|
|
// {
|
|
// if (bindingContext.ModelType == typeof(string))
|
|
// {
|
|
// var value = base.BindModel(controllerContext, bindingContext);
|
|
|
|
// return value?.ToString().Trim();
|
|
// }
|
|
|
|
// var special = bindingContext.ModelType.IsArray;
|
|
// if (!special && bindingContext.ModelType.IsGenericType && bindingContext.ModelType.GenericTypeArguments.All(t => t.IsValueType))
|
|
// {
|
|
// var typeDef = bindingContext.ModelType.GetGenericTypeDefinition();
|
|
// special = typeDef == typeof(IEnumerable<>) || typeDef == typeof(List<>);
|
|
// }
|
|
// if (special)
|
|
// {
|
|
// var bindingContext2 = new ModelBindingContext
|
|
// {
|
|
// ModelMetadata = bindingContext.ModelMetadata,
|
|
// ModelState = bindingContext.ModelState,
|
|
// PropertyFilter = bindingContext.PropertyFilter,
|
|
// FallbackToEmptyPrefix = bindingContext.FallbackToEmptyPrefix,
|
|
// //Model = bindingContext.Model,
|
|
// ModelName = bindingContext.ModelName,
|
|
// //ModelType = bindingContext.ModelType
|
|
// };
|
|
|
|
// if (!controllerContext.Controller.ValidateRequest && bindingContext.ValueProvider is IUnvalidatedValueProvider provider)
|
|
// {
|
|
// bindingContext2.ValueProvider = new UnvalidatedValueProvider(provider);
|
|
// }
|
|
// else
|
|
// {
|
|
// bindingContext2.ValueProvider = new ValueProvider(bindingContext.ValueProvider);
|
|
// }
|
|
|
|
// return base.BindModel(controllerContext, bindingContext2);
|
|
// }
|
|
|
|
// return base.BindModel(controllerContext, bindingContext);
|
|
// }
|
|
|
|
// protected override void SetProperty(ControllerContext controllerContext,
|
|
// ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value)
|
|
// {
|
|
// if (propertyDescriptor.PropertyType == typeof(string))
|
|
// {
|
|
// try
|
|
// {
|
|
// //下面这行代码应该有用,但确实不知道起什么作用了,另外这行代码有问题
|
|
// //问题:如果绑定对象的属性和内部对象绑定属性一致, 那么取出来的值是主对象的
|
|
// //知道起什么作用了:如果action绑定时有IFormCollection对象,那么其它对象将不会赋值,sb思路
|
|
// //var val = controllerContext.RequestContext.HttpContext.Request[propertyDescriptor.Name];
|
|
// //应该修正为
|
|
// var val = string.IsNullOrEmpty(bindingContext.ModelName)
|
|
// ? controllerContext.RequestContext.HttpContext.Request[propertyDescriptor.Name]
|
|
// : controllerContext.RequestContext.HttpContext.Request[$"{bindingContext.ModelName}[{propertyDescriptor.Name}]"];
|
|
// if (!string.IsNullOrEmpty(val))
|
|
// {
|
|
// value = val.Trim();
|
|
// }
|
|
// }
|
|
// catch
|
|
// {
|
|
|
|
// }
|
|
// }
|
|
|
|
// base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
|
|
// }
|
|
|
|
// protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
|
|
// {
|
|
// if (bindingContext.Model is PagedQueryModel model)
|
|
// {
|
|
// switch (propertyDescriptor.Name)
|
|
// {
|
|
// case nameof(PagedQueryModel.PageIndex):
|
|
// model.PageIndex = int.TryParse(controllerContext.RequestContext.HttpContext.Request["page"], out int pageIndex) ? pageIndex : 1;
|
|
// return;
|
|
// case nameof(PagedQueryModel.PageSize):
|
|
// model.PageSize = int.TryParse(controllerContext.RequestContext.HttpContext.Request["rows"], out int pageSize) ? pageSize : 25;
|
|
// return;
|
|
// case nameof(PagedQueryModel.SortExpression):
|
|
// model.SortExpression = controllerContext.RequestContext.HttpContext.Request["sidx"];
|
|
// if (!string.IsNullOrWhiteSpace(model.SortExpression))
|
|
// {
|
|
// model.SortExpression += " " + controllerContext.RequestContext.HttpContext.Request["sord"];
|
|
// }
|
|
// return;
|
|
// }
|
|
// }
|
|
// base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
|
|
// }
|
|
|
|
// struct ValueProvider : IValueProvider
|
|
// {
|
|
// readonly IValueProvider _provider;
|
|
|
|
// public ValueProvider(IValueProvider provider)
|
|
// {
|
|
// _provider = provider;
|
|
// }
|
|
|
|
// public bool ContainsPrefix(string prefix)
|
|
// {
|
|
// return _provider.ContainsPrefix(prefix);
|
|
// }
|
|
|
|
// public ValueProviderResult GetValue(string key)
|
|
// {
|
|
// var value = _provider.GetValue(key);
|
|
|
|
// if (value?.RawValue == null)
|
|
// {
|
|
// return value;
|
|
// }
|
|
|
|
// if (string.IsNullOrWhiteSpace(value.AttemptedValue))
|
|
// {
|
|
// return new ValueProviderResult(null, null, System.Globalization.CultureInfo.InvariantCulture);
|
|
// }
|
|
|
|
// return value;
|
|
// }
|
|
// }
|
|
|
|
// struct UnvalidatedValueProvider : IValueProvider
|
|
// {
|
|
// readonly IUnvalidatedValueProvider _provider;
|
|
|
|
// public UnvalidatedValueProvider(IUnvalidatedValueProvider provider)
|
|
// {
|
|
// _provider = provider;
|
|
// }
|
|
|
|
// public bool ContainsPrefix(string prefix)
|
|
// {
|
|
// return _provider.ContainsPrefix(prefix);
|
|
// }
|
|
|
|
// public ValueProviderResult GetValue(string key)
|
|
// {
|
|
// var value = _provider.GetValue(key, true);
|
|
|
|
// if (value?.RawValue == null)
|
|
// {
|
|
// return value;
|
|
// }
|
|
|
|
// if (string.IsNullOrWhiteSpace(value.AttemptedValue))
|
|
// {
|
|
// return new ValueProviderResult(null, null, System.Globalization.CultureInfo.InvariantCulture);
|
|
// }
|
|
|
|
// return value;
|
|
// }
|
|
// }
|
|
// }
|
|
//} |