ASP.NET MVC4系列验证机制、伙伴类共享源数据信息(数据注解和验证)
一,mvc前后台验证
自定义属性标签MyRegularExpression
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web; namespace MvcAjaxValidate.Models
{
public class MyRegularExpression:RegularExpressionAttribute
{
//为了多次修改正则,我们直接写一个类,只改这个地方就好//勿忘global文件
//DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(MyRegularExpression), typeof(RegularExpressionAttributeAdapter)); public MyRegularExpression() : base(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$") { }
}
}
要起作用,需要在global文件注册适配器
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(MyRegularExpression), typeof(RegularExpressionAttributeAdapter));
自定义model:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MvcAjaxValidate.Models
{
public class DemoEntity
{
[Required(ErrorMessage = "*必填")]
public int id { get; set; } [StringLength(, ErrorMessage = "*长度小于10")]
//[RegularExpression(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "必须邮箱")]
[MyRegularExpression(ErrorMessage = "必须邮箱")]
public string name { get; set; } // [Range(5, 10,ErrorMessage="5-10")]//数值,而不是长度 [StringLength(,ErrorMessage="最大5个字符")]
//[MinLength(2,ErrorMessage="最小2个字符")]//不起作用,改成正则形式
[RegularExpression(@"^.{2,}", ErrorMessage = "最小2个字符")]
[DataType(DataType.Password)]
public string password { get; set; } [DataType(DataType.Password)]
[Compare("password", ErrorMessage = "密码要一致")]
public string passwordRe { get; set; }
}
}
controller action
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAjaxValidate.Models; namespace MvcAjaxValidate.Controllers
{
public class DemoEntityController : Controller
{
//
// GET: /DemoEntity/ public ActionResult create(DemoEntity demo)
{
if (ModelState.IsValid)
{ }
return View();
} }
}
view(create强类型视图)
@model MvcAjaxValidate.Models.DemoEntity
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>create</title>
</head>
<body>
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script>
function complete() {
alert()
}
</script>
@using (Ajax.BeginForm("create", new AjaxOptions() {HttpMethod="post", Confirm="ok?" , OnComplete="complete"}))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>DemoEntity</legend>
<div class="editor-label">
@Html.LabelFor(model => model.name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.name)
@Html.ValidationMessageFor(model => model.name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.password)
@Html.ValidationMessageFor(model => model.password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.passwordRe)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.passwordRe)
@Html.ValidationMessageFor(model => model.passwordRe)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</body>
</html>
二,常为ef自动生成实体类采用伙伴类的技术验证
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;//...
using System.Linq;
using System.Web; namespace MvcAjaxValidate.Models
{ //伙伴类的技术,共享源数据信息
//防止ef实体修改后,标签出被冲掉,我们换一个地方写。ef变 标签写到外面了,就不会丢了
//让实体类和目标类共享信息,在这个类的所有属性,目标类就会有
//这个类和目标类属性名一样哦
//实体类和目标类要在一个命名空间!!!!!!!!!!!!!!!!
[MetadataType(typeof(MetaTypeShare))]
public partial class UserInfor
{ }
//目标类
public class MetaTypeShare
{
public int ID { get; set; }
[Required(ErrorMessage="必填")]
public string UName { get; set; }
public string UPassword { get; set; }
public Nullable<System.DateTime> USubTime { get; set; }
public Nullable<System.DateTime> ULastMoltifyTime { get; set; }
public Nullable<System.DateTime> ULastLoginTime { get; set; }
public string UEmail { get; set; }
public string UAddress { get; set; }
public string UPhone { get; set; }
public string URemark { get; set; }
public Nullable<short> Usex { get; set; }
public Nullable<short> UDelFlag { get; set; }
public Nullable<int> UErrorCount { get; set; }
} }
ASP.NET MVC4系列验证机制、伙伴类共享源数据信息(数据注解和验证)的更多相关文章
- 【ASP.NET MVC系列】浅谈数据注解和验证
[ASP.NET MVC系列]浅谈数据注解和验证 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google C ...
- ASP.NET CORE系列【六】Entity Framework Core 之数据迁移
原文:ASP.NET CORE系列[六]Entity Framework Core 之数据迁移 前言 最近打算用.NET Core写一份简单的后台系统,来练练手 然后又用到了Entity Framew ...
- asp.net mvc常用的数据注解和验证以及entity framework数据映射
终于有时间整理一下asp.net mvc 和 entity framework 方面的素材了. 闲话少说,步入正题: 下面是model层的管理员信息表,也是大伙比较常用到的,看看下面的代码大伙应该不会 ...
- MVC中的数据注解和验证
数据注解和验证 用户输入验证在客户端浏览器中需要执行验证逻辑. 在客户端也需要执行. 注解是一种通用机制, 可以用来向框架注入元数据, 同时, 框架不只驱动元数据的验证, 还可以在生成显示和编辑模型的 ...
- MVC5 数据注解和验证
①利用数据注解进行验证 ②创建自定义的验证逻辑 ③模型元数据注解的用法 ①先创建数据源 1,创建我们的Model Order 2,创建控制器带EF 选择模型为Order 当你运行的时候会报错,需要代 ...
- MVC学习手册之数据注解与验证
MVC学习手册之数据注解与验证 新建一个MVC5的WEB应用程序,VS2013会自动生成一段代码,以下是Account控制器下Register.cshtml 页面的代码: @model WebAppl ...
- 数据注解和验证 – ASP.NET MVC 4 系列
不仅在客户端浏览器中需要执行验证逻辑,在服务器端也需要执行.客户端验证能即时给出一个错误反馈(阻止请求发送至服务器),是时下 Web 应用程序所期望的特性.服务器端验证,主要是因为来自网 ...
- ASP.NET MVC5----常见的数据注解和验证
只要一直走,慢点又何妨. 在使用MVC模式进行开发时,数据注解是经常使用的(模型之上操作),下面是我看书整理的一些常见的用法. 什么是验证,数据注解 验证 从全局来看,发现逻辑仅是整个验证的很小的一部 ...
- Asp.net MVC]Asp.net MVC5系列——实现编辑、删除与明细信息视图
目录 概述 实现信息的明细视图 实现信息的编辑视图 实现信息的删除视图 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net ...
随机推荐
- IE的浏览器模式和文档模式
只有IE浏览器中才会有“浏览器模式”和“文档模式”,兼容性视图涉及两个重要的功能 便是“浏览器模式[browser mode]”和“文档模式[document mode]”,在IE8/IE9中按F12 ...
- vs c++ 将string转换为double
可以用atof()这个函数,但是这个函数的参数是char*类型的,因此需将string类型强制转换,方法为在函数的参数中写成 const_cast<const char *>(str.c_ ...
- Android之智能问答机器人
本文主要利用图灵机器人的接口,所做的一个简单的智能问答机器人 实现 由于发送与接收消息都是不同的listView,所以要用有两个listVeiw的布局文件 接收消息布局文件 <?xml vers ...
- 以普通用户登录 su root 用vncviewer:xxxxx 会报错!!exit 回到最初环境变的用户 问题解决!!!!
[root@ok IT-DOC]# vncviewer : TigerVNC Viewer - built May :: Copyright (C) - TigerVNC Team and many ...
- innobackupex err2
报错: [root@DB dbdata]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=123 /data/dbda ...
- THINKPHP 默认模板路径替换
APP_PATH // 当前项目目录APP_NAME // 当前项目名称 ACTION_NAME // 当前操作名称 CACHE_PATH // 项目模版缓存目录 CONFIG_PATH //项目配置 ...
- 手把手教你在Windows下使用MinGW编译libav(参考libx264的编入)
转自:http://www.th7.cn/Program/cp/201407/242762.shtml 手把手教你在Windows下使用MinGW编译libav libav是在Linux下使用纯c语言 ...
- InputStream的三个read的区别
转自:http://www.blogjava.net/toby/archive/2009/04/24/267413.html 1.read这个方法是对这个流一个一个字节的读,返回的int就是这个字节的 ...
- SQLServer2008默认服务配置
SQLServer2008默认服务配置
- SU Demos-03T-F Analysis-01Sugabor
先看readme, 运行结果,