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 ...
随机推荐
- [Android Pro] Android fastboot刷机和获取Root权限
参考文章: https://developers.google.com/android/nexus/images 转载自: http://www.inexus.co/article-1280-1 ...
- 解决Unable to reach a settlement: [diffie-hellman-group1-sha1, diffie-hellman-group-exchange-sha1] and [curve25519-sha256@li
SharpSSH或JSCH使用diffie-hellman-group1-sha1和diffie-hellman-group-exchange-sha1密钥交换算法,而OpenSSH在6.7p1版本之 ...
- Fence Repair(poj 3253)
题意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个要求的小木板的长度,及小木板的个数n,求最小费用 提示: 以 3 5 8 5 ...
- oracle 10g 学习之oracle管理(3)
怎样将预先写好的sql脚本执行? select * from employees;→107条记录 利用 Oracle 企业管理器连接数据库服务器 点击打开以下界面: 此时已经连接成功了 用 Oracl ...
- hdu 4584 水题爽一发 *
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #i ...
- C++学习网站总结(转)
总结帖: http://club.topsage.com/thread-361504-1-1.html Visual C++ (VC) / MFC 电子书下载: Visual C++ 2008 入门经 ...
- hdu 1430+hdu 3567(预处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路:由于只是8种颜色,所以标号就无所谓了,对起始状态重新修改标号为 12345678,对目标状 ...
- 在mysql数据库中制作千万级测试表
在mysql数据库中制作千万级测试表 前言: 最近准备深入的学一下mysql,包括各种引擎的特性.性能优化.分表分库等.为了方便测试性能.分表等工作,就需要先建立一张比较大的数据表.我这里准备先建一张 ...
- Selenium介绍
基于selenium的自动化测试,华为已经做了两代了,目前是做到SmartGUI2.0,基于他们自己的AutoSpace平台.不过目前支持的貌似只有IE和火狐(火狐3.6).控件录制支持ID,Name ...
- hdu 5753 Permutation Bo
这里是一个比较简单的问题:考虑每个数对和的贡献.先考虑数列两端的值,两端的摆放的值总计有2种,比如左端:0,大,小:0,小,大:有1/2的贡献度.右端同理. 中间的书总计有6种可能.小,中,大.其中有 ...