直接贴代码了:

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 = "保存成功" });}}
}

谢谢浏览!

转载于:https://www.cnblogs.com/Music/p/comma-separated-model-binder-in-asp-net-mvc.html

ASP.NET MVC 自定义模型绑定1 - 自动把以英文逗号分隔的 ID 字符串绑定成 Listint...相关推荐

  1. ASP.NET MVC 自定义Razor视图WorkContext

    概述 1.在ASP.NET MVC项目开发的过程中,我们经常需要在cshtml的视图层输出一些公用信息 比如:页面Title.服务器日期时间.页面关键字.关键字描述.系统版本号.资源版本号等 2.普通 ...

  2. ASP.NET MVC 自定义路由中几个需要注意的小细节

    本文主要记录在ASP.NET MVC自定义路由时,一个需要注意的参数设置小细节. 举例来说,就是在访问 http://localhost/Home/About/arg1/arg2/arg3 这样的自定 ...

  3. Asp.Net Mvc之模型注解

    Asp.Net Mvc之模型注解 正文: 命名空间: using System.ComponentModel;      using System.ComponentModel.DataAnnotat ...

  4. asp.net mvc 自定义 pager 封装与优化

    asp.net mvc 自定义 pager 封装与优化 Intro 之前做了一个通用的分页组件,但是有些不足,从翻页事件和分页样式都融合在后台代码中,到翻页事件可以自定义,再到翻页和样式都和代码分离, ...

  5. 第三章 ASP.NET MVC (模型绑定)

    一.约定胜于配置 ASP.NET有一个默认模型绑定机制,当点击表单中submit按钮或a标签时,会将相应表单的数据提交并跳转到到action内指定的网址或href属性内指定的网址,当跳转到指定网址前, ...

  6. ASP.NET MVC 2 模型验证

    2019独角兽企业重金招聘Python工程师标准>>> [原文地址]ASP.NET MVC 2: Model Validation  [原文发表日期] Friday, January ...

  7. ASP.net MVC自定义错误处理页面的方法

    在ASP.NET MVC中,我们可以使用HandleErrorAttribute特性来具体指定如何处理Action抛出的异常.只要某个Action设置了HandleErrorAttribute特性,那 ...

  8. 【转】ASP.net MVC自定义错误处理页面的方法

    在ASP.NET MVC中,我们可以使用HandleErrorAttribute特性来具体指定如何处理Action抛出的异常.只要某个Action设置了HandleErrorAttribute特性,那 ...

  9. Asp.Net MVC 自定义的MVC框架(非EF操作数据库)

    一些废话:在北京辞职回家不知不觉中已经半年多了,这半年中有过很多的彷徨,困惑,还有些小小难受.半年时间算是我人生以来遇到过的最困苦的时候.理想的工作跟我擦肩而过,驾照也没有考过,年后这一改革...,毕 ...

最新文章

  1. yum -y --downloadonly --downloaddir=/ruiy upggrde;
  2. 用EXCEL实现时间戳格式和日期格互转
  3. 如何使用cocos2dx-jsbinding 来处理分辨率适配
  4. CSS3 矢量图标及背景精灵
  5. Gstreamer离线版官方文档(十五)
  6. CodeProject终于迁到Asp.Net了
  7. Windows下Cmake安装步骤详解(图文)
  8. SCI收录期刊——声学学科 (转载)
  9. packet和frame区别
  10. linux apktool的用法,Apktool的基本用法
  11. 一个img文件-实验吧
  12. deny后加to do还是doing_常见的后面只能接to do的词组(不能接doing)有哪些
  13. 小米路由修改服务器密码,192.168.31.1小米路由器修改WIFI密码
  14. 这部计算机27厘米宽英语怎么说,24/27/32英寸电脑显示器的尺寸是多少?长和宽几厘米?显示长度,宽度和面积的算法...
  15. JAVA————简单的图形绘制界面(教程)
  16. 关于iOS的自动弹出键盘问题
  17. 【未】Optimizing Rebalance Scheme for Dock-less Bike Sharing Systems with Adaptive User Incentive
  18. Android S 新特性:应用行为更改
  19. HTML链接打开腾讯QQ聊天窗口,超链接打开qq对话框
  20. 通用一键打包软件,数据包制作工具

热门文章

  1. chrome jquery ajax请求,jQuery.ajax在Chrome中无法正常执行的解决办法
  2. python2中可以使用print()函数吗_在Python2.x中使用print()(函数版本)
  3. win7局域网共享设置_Win7系统打印机共享不了的设置方法思路!
  4. LQ训练营(C++)学习笔记_栈与递归
  5. git 安装包_Git、TortoiseGit的安装、使用说明
  6. 算法分析设计--递归算法
  7. ACM-ICPC 2019 山东省省赛 C Wandering Robot
  8. Spring MVC 基于注解的class文件打成jar包后注入失败
  9. 高手经验:一个新手的verilog学习经验
  10. shell 删除乱码文件