首页 > 代码库 > asp.net mvc int[] 和 string[] 自定义数组绑定

asp.net mvc int[] 和 string[] 自定义数组绑定

新建类,int[]数组模型绑定

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace Koukou.Admin.ModelBinder{    public class IntArrayModelBinder : DefaultModelBinder    {        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)        {            var value = http://www.mamicode.com/bindingContext.ValueProvider.GetValue(bindingContext.ModelName);>

 

string[] 数组模型绑定

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace Koukou.Admin.ModelBinder{    public class StringArrayModelBinder : DefaultModelBinder    {        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)        {            var value = http://www.mamicode.com/bindingContext.ValueProvider.GetValue(bindingContext.ModelName);",", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);            // 逗号“,” 和 换行符 作为分隔        }    }}

 

在global.asax的Application_Start注册

protected void Application_Start()        {            AreaRegistration.RegisterAllAreas();            RegisterGlobalFilters(GlobalFilters.Filters);            RegisterRoutes(RouteTable.Routes);//自定义model绑定            ModelBinders.Binders.Add(typeof(int[]), new ModelBinder.IntArrayModelBinder());            ModelBinders.Binders.Add(typeof(string[]), new ModelBinder.StringArrayModelBinder());        }

  

  

asp.net mvc int[] 和 string[] 自定义数组绑定