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 = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (value == null || string.IsNullOrEmpty(value.AttemptedValue))
{
return null;
} return value
.AttemptedValue
.Split(',')
.Select(int.Parse)
.ToArray();
}
}
}
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 = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (value == null || string.IsNullOrEmpty(value.AttemptedValue))
{
return null;
} return value
.AttemptedValue
.Split(new string[] { ",", 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[] 自定义数组绑定的更多相关文章
- ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET MVC 学习笔记-6.异步控制器 ASP.NET MVC 学习笔记-5.Controller与View的数据传递 ASP.NET MVC 学习笔记-4.ASP.NET MVC中Ajax的应用 ASP.NET MVC 学习笔记-3.面向对象设计原则
ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET程序中的web.config文件中,在appSettings这个配置节中能够保存一些配置,比如, 1 <appSettin ...
- ASP.NET MVC 中如何用自定义 Handler 来处理来自 AJAX 请求的 HttpRequestValidationException 错误
今天我们的项目遇到问题 为了避免跨站点脚本攻击, 默认我们项目是启用了 validateRequest,这也是 ASP.NET 的默认验证规则.项目发布后,如果 customError 启用了,则会显 ...
- ASP.NET MVC应用安全性(一)——自定义错误处理
很多 ASP.NET MVC 开发者都会写出高性能的代码,很好地交付软件,等等.但是却并没有安全性方面的计划. 有一种攻击是攻击者截获最终用户提交的表单数据,将其改变再将修改后的数据发送到服务器. ...
- ASP.NET MVC 系统过滤器、自定义过滤器
一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...
- [转]Asp.net MVC 利用PartialView 构造自定义菜单
本文转自:http://www.cnblogs.com/huyq2002/archive/2012/01/06/2314838.html 在VS2010中利用Asp.net MVC自带的模板生成的菜单 ...
- ASP.NET MVC学习笔记-----使用自定义的View Engine
我们都知道在ASP.NET MVC中自带了Razor View Engine,Razor十分的强大,可以满足我们绝大部分的需要.但是ASP.NET MVC的高度可扩展性,使我们可以使用自定义的View ...
- 4.ASP.NET MVC 5.0 视图之模型绑定
大家好,这篇文章,我将向大家介绍ASP.NET MVC的模型视图绑定,ASP.MVC的模型绑定分为两种:一种是动态绑定[Dynamic Binding];还有一种就是强类型绑定[Strongly ty ...
- Asp.Net MVC在过滤器中使用模型绑定
废话不多话,直接上代码 1.创建MVC项目,新建一个过滤器类以及使用到的实体类: public class DemoFiltersAttribute : AuthorizeAttribute { pu ...
- ASP.NET MVC 3 使用Model自定义验证的样式
1.修改jquery.validate.unobtrusive.js 将onError方法修改 //修改的部分 //////////////////////////////////////////// ...
随机推荐
- Centos是什么
Linux是GNU/Linux的缩写,通常指各种Linux发行版的通称. 常见的Linux厂家主要有Redhat/Novell等. Redhat有两大Linux产品系列,其一是免费的Fedora Co ...
- memcached安装和php-memcached扩展安装.update.2014-08-15
服务器端主要是安装memcache服务器端,目前的最新版本是 memcached-1.3.0 .下载官网:http://www.danga.com另外,Memcache用到了libevent这个库用于 ...
- apache LogFormat参数说明
在apache的配置文件httpd.conf里一般都有类似于LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Refere ...
- Js日期选择器并自动加入到输入框中
<html> <head> <title>Js日期选择器并自动加入到输入框中</title> <meta http-equiv="con ...
- C++_Eigen函数库用法笔记——The Array class and Coefficient-wise operations
The advantages of Array Addition and subtraction Array multiplication abs() & sqrt() Converting ...
- 字符串模拟赛T2
// source code from laekov for c0x17 #define PRID "fkqh" #include <cstdio> #include ...
- Y2K Accounting Bug(贪心)
Y2K Accounting Bug Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10945 Accepted: 54 ...
- CloudPlatform和CloudStack的关系
The Scalr team is at the CloudStack Collab Conf, and this post summarizes a few things we learned. C ...
- 在html中注释对 <include XXXXXXXX />是没有影响的
在html中注释对 <include XXXXXXXX />是没有影响的
- 《ASP.NET MVC4 WEB编程》学习笔记------Web API
本文截取自情缘 1. Web API简单说明 近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集 ...