MVC扩展ModelBinder使类型为DateTime的Action参数可以接收日期格式的字符串
原文:MVC扩展ModelBinder使类型为DateTime的Action参数可以接收日期格式的字符串
如何让视图通过某种途径,把符合日期格式的字符串放到路由中,再传递给类型为DateTime的控制器方法参数?即string→DateTime。MVC默认的ModelBinder并没有提供这样的机制,所以我们要自定义一个ModelBinder。
首先,在前台视图中,把符合日期格式的字符串赋值给date变量放在路由中:
@Html.ActionLink("传入日期格式为2014-06-19","Date",new {date = "2014-06-19"})
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
控制器方法中,希望这样接收date这个变量:
public ActionResult Date(DateTime date)
{
ViewData["Date"] = date;
return View();
}
自定义的ModelBinder实现IModelBinder接口:
using System;
using System.Web.Mvc; namespace MvcApplication1.Extension
{
public class DateTimeModelBinder : IModelBinder
{
public string Format { get; private set; } public DateTimeModelBinder(string format)
{
this.Format = format;
} public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
//从ValueProvider中,模型名称为key,获取该模型的值
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
return DateTime.ParseExact((string)value.AttemptedValue, this.Format, null);
}
}
}
以上,通过构造函数注入格式,通过属性使用格式,在BindModel()方法中取出ValueProvider中的值,再转换成DateTime类型。
接下来的问题是:DateTime date如何才能用上自定义的ModelBinder呢?为此,我们需要一个派生于CustomModelBinderAttribute的类,重写CustomModelBinderAttribute的GetBinder()方法。
using System.Web.Mvc; namespace MvcApplication1.Extension
{
public class DateTimeAttribute : CustomModelBinderAttribute
{
public string Format { get; private set; } public DateTimeAttribute(string format)
{
this.Format = format;
} public override IModelBinder GetBinder()
{
return new DateTimeModelBinder(this.Format);
}
}
}
再把DateTimeAttribute打到控制器方法参数上:
public ActionResult Date([DateTime("yyyy-MM-dd")]DateTime date)
{
ViewData["Date"] = date;
return View();
}
于是,最终可以在视图中这样使用从控制器方法传来的、放在ViewData中DateTime类型:
@{
var date = (DateTime) ViewData["Date"];
}
<span>接收到的日期是:</span>
<span>@date.ToShortDateString()</span>
MVC扩展ModelBinder使类型为DateTime的Action参数可以接收日期格式的字符串的更多相关文章
- MVC扩展ModelBinder,通过继承DefaultModelBinder把表单数据封装成类作为action参数
把视图省.市.街道表单数据,封装成一个类,作为action参数.如下: action方法参数类型: namespace MvcApplication1.Models{ public class ...
- WebApi接收接收日期格式参数时,日期类型(2019-10-08T16:00:00.000Z)后台接收时间少8小时问题
前端使用的是elementui的日期控件,将日期格式的数据提交到webapi后台时,接收到的日期格式少了8小时,这个原因是由于时区引起的,应该在WebApiConfig进行配置转成本地时间,解决少8小 ...
- MVC扩展控制器工厂,通过实现IControllerFactory,根据action名称生成不同的Controller
关于控制器工厂的扩展,要么通过实现IControllerFactory接口,要么通过继承DefaultControllerFactory.本篇中,我想体验的是: 1.当请求经过路由,controlle ...
- POI使用:用poi接口不区分xls/xlsx格式解析Excel文档(41种日期格式解析方法,5种公式结果类型解析方法,3种常用数值类型精度控制办法)
一.使用poi解析excel文档 注:全部采用poi接口进行解析,不需要区分xls.xlsx格式,不需要判断文档类型. poi中的日期格式判断仅支持欧美日期习惯,对国内的日期格式并不支持判断,怎么办? ...
- 17+个ASP.NET MVC扩展点【附源码】
1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中!即:创建一个实现了IHttpmodule接口的类,并将配置WebConfig. 在自定义的Http ...
- MVC 扩展 Html.ImageFor
Asp.Net MVC 扩展 Html.ImageFor 方法详解 背景: 在Asp.net MVC中定义模型的时候,DataType有DataType.ImageUrl这个类型,但htmlhelpe ...
- C# MVC 用户登录状态判断 【C#】list 去重(转载) js 日期格式转换(转载) C#日期转换(转载) Nullable<System.DateTime>日期格式转换 (转载) Asp.Net MVC中Action跳转(转载)
C# MVC 用户登录状态判断 来源:https://www.cnblogs.com/cherryzhou/p/4978342.html 在Filters文件夹下添加一个类Authenticati ...
- NotSupportedException-无法将类型“System.DateTime”强制转换为类型“System.Object”
几张图就可以说明一切 2015-03-29 21:54:09,206 [77] ERROR log - System.NotSupportedException: 无法将类型“System.DateT ...
- Asp.net 面向接口可扩展框架之“Mvc扩展框架及DI”
标题“Mvc扩展框架及DI”有点绕口,我也想不出好的命名,因为这个内容很杂,涉及多个模块,但在日常开发又密不可分 首先说Mvc扩展框架,该Mvc扩展就是把以前的那个Mvc分区扩展框架迁移过来,并优化整 ...
随机推荐
- JListDemo
Tips: (1)JList不能自动滚动,要想为列表框加上滚动条,必须将JList插入到一个JScrollPane中,然后将JScollPane而不是JList,插入到外围JPanel上 (2)Lis ...
- Machine Learning—Linear Regression
Evernote的同步分享:Machine Learning-Linear Regression 版权声明:本文博客原创文章.博客,未经同意,不得转载.
- Following unknown configure options were used:--enable-fpm
跑cd php-5.2.13安装时间 ./configure --prefix=/usr/local/php/ --with-config-file-path=/usr/local/php/etc ...
- T-SQL开发——ID处理篇
原文:T-SQL开发--ID处理篇 数据库自增ID功能中Identity.Timestamp.Uniqueidentifier的区别: 问题现象: 一般序号的产生,对于一般程序员而言,都是使用T-SQ ...
- String.Format in Java and C#
原文:String.Format in Java and C# JDK1.5中,String类新增了一个很有用的静态方法String.format(): format(Locale l, String ...
- Maven: NoClassDefFoundError: org/codehaus/plexus/classworlds/launcher/Launcher
为了和团队开发环境保持一致,须要 在Ubuntu上安装maven2.2.1,引文我之前已经用apt-get命令安装了3.3的maven.在运行maven命令时报错: Maven: NoClassDef ...
- MVC 如何在一个同步方法(非async)方法中等待async方法
MVC 如何在一个同步方法(非async)方法中等待async方法 问题 首先,在ASP.NET MVC 环境下对async返回的Task执行Wait()会导致线程死锁.例: public Actio ...
- Oracle查询速度慢的原因总结
Oracle查询速度慢的原因总结 查询速度慢的原因很多,常见如下几种:1,没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷)2,I/O吞吐量小,形成了瓶颈效应.3,没有创建计算列导致 ...
- 采用xshell链路本地虚拟机Linux
昨天想安装在自己的机器看Linux.而使用xshell通路.但是这花了很长的时间,于xshell结束所有的提示"Could not connect to '192.168.54.100' ( ...
- js 通信
js 页面间的通信 看了一下公司原来的代码,原页面ajax post返回一个页面完整的HTML,然后再打开一个新页面并输出ajax返回的所有代码到新页面上,在新页面上以表单提交的形式实现重定向. 任凭 ...