zoukankan      html  css  js  c++  java
  • ASP.NET MVC 自定义模型绑定1

    直接贴代码了:

    CommaSeparatedModelBinder.cs

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Web.Mvc;
    
    namespace MvcSample.Extensions
    {
        public class CommaSeparatedModelBinder : DefaultModelBinder
        {
            private static readonly MethodInfo ToArrayMethod = typeof(Enumerable).GetMethod("ToArray");
    
            public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
            {
                return BindCsv(bindingContext.ModelType, bindingContext.ModelName, bindingContext)
                        ?? base.BindModel(controllerContext, bindingContext);
            }
    
            protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
            {
                return BindCsv(propertyDescriptor.PropertyType, propertyDescriptor.Name, bindingContext)
                        ?? base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
            }
    
            private object BindCsv(Type type, string name, ModelBindingContext bindingContext)
            {
                if (type.GetInterface(typeof(IEnumerable).Name) != null)
                {
                    var actualValue = bindingContext.ValueProvider.GetValue(name);
    
                    if (actualValue != null)
                    {
                        var valueType = type.GetElementType() ?? type.GetGenericArguments().FirstOrDefault();
    
                        if (valueType != null && valueType.GetInterface(typeof(IConvertible).Name) != null)
                        {
                            var list = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(valueType));
    
                            foreach (var splitValue in actualValue.AttemptedValue.Split(new[] { ',' }))
                            {
                                if (!String.IsNullOrWhiteSpace(splitValue))
                                    list.Add(Convert.ChangeType(splitValue, valueType));
                            }
    
                            if (type.IsArray)
                                return ToArrayMethod.MakeGenericMethod(valueType).Invoke(this, new[] { list });
                            
                            return list;
                        }
                    }
                }
    
                return null;
            }
        }
    }

    客户端测试 :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace MvcSample.Controllers
    {
        public class HomeController : Controller
        {
            [HttpPost]
            public ActionResult ModifyProduct(int productId, int newYear,
                [ModelBinder(typeof(CommaSeparatedModelBinder))] List<int> orderStatusIds = null)
            {
                ProductInfoService.TryModifyById(productId, newYear);
                return Json(new { Success = true, Message = "保存成功" });
            }
        }
    }

    谢谢浏览!

  • 相关阅读:
    小程序动态修改页面标题setNavigationBarTitle
    webapi发布在iis之后报错Http 403.14 error
    vue调用子组件方法时,参数传不过去
    Echarts中X轴坐标太密集,分段显示
    使用echarts时,鼠标首次移入屏幕会闪动,全屏会出现滚动条
    js关于数组的操作(合并数组、添加数组、循环等)
    在vue项目中使用echarts
    npm i安装命令中的-g -D -S的区别
    ArcGIS api for JS 实现三维飞行漫游功能
    Vue通过EventBus实现兄弟组件间通信
  • 原文地址:https://www.cnblogs.com/Music/p/comma-separated-model-binder-in-asp-net-mvc.html
Copyright © 2011-2022 走看看