控制器 Home

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MvcApplication3.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "修改此模板以快速启动你的 ASP.NET MVC 应用程序。"; return View();
} /// <summary>
/// 检查用户名是否有重复
/// </summary>
/// <param name="userName">用户在页面(视图)表单中输入的UserName</param>
/// <returns>Json</returns>
public ActionResult CheckUserName(string userName)
{
bool result = true; if (userName == "admin")
result = false;//已存在 return Json(result, JsonRequestBehavior.AllowGet);
}
}
}

Model(UserInfo)

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; using System.Web; using System.Web.Mvc;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace Model
{
/// <summary>
///  这些例子就是验证的,注意此处没有客户端验证,完全是服务器端验证的,如果出错,视图会渲染再次显示,非常好用啊,很方便啊,要在以前你要写多少行代码来搞定这些繁琐无味的验证呢?微软太体恤程序员了,顶一个。
///  在下面的例子当中,当数据发送到服务器验证不通过之后,则所有用户填写的内容在重新打开的页面后都清空了, 如果想不清空,即用户第一次填的内容还存在的话,可以return View(接受到的对象);
/// </summary>
public class UserInfo
{
public int Id { get; set; } //ID
/// <summary>
///DisplayName:指定当前字段显示的名称
///StringLength:指定允许的长度,括号里的第一个参数是最大长度,第二个参数是最小长度。最后一个参数是指:单字段值的长度不符合第一第二参数规定时候,提示错误的内容
///Required:表示当前字段是必填选项,当提交的表单缺少该值就引发验证错误。括号里的AllowEmptyStrings是: 获取或设置一个值,该值指示是否允许空字符串。单值为false的时候表示当前字段的值不能为空
///Remote:允许利用服务器端的回调函数执行客户端的验证逻辑。说白了就是支持AJAX验证。括号里的CheckUserName是Action方法。Home是控制器 ,它会调用Home控制中的CheckUserName方法来查询你所输入的用户名是否已经存在,如果存在,则提示"用户名已经存在"
/// </summary>
[DisplayName("用户名")]
[Required(AllowEmptyStrings = false, ErrorMessage = "用户名不能为空")]
[StringLength(, MinimumLength = , ErrorMessage = "用户名长度必须在{2}和{1}位之间")]
[Remote("CheckUserName", "Home", ErrorMessage = "用户名已经存在")]
public string UserName { get; set; } //用户名 [DisplayName("用户密码")]
[DataType(DataType.Password)] //将密码已*号的形式来显示
[Required(AllowEmptyStrings = false, ErrorMessage = "密码不能为空")]
[StringLength(, MinimumLength = , ErrorMessage = "密码长度必须在{2}和{1}位之间")]
public string UserPassword { get; set; } //用户密码 [DataType(DataType.Password)]
[System.ComponentModel.DataAnnotations.Compare("UserPassword", ErrorMessage = "密码不一致")]
[DisplayName("请再次确认密码")]
public string TUserPassword { get; set; } //再次输出密码 /// <summary>
/// RegularExpression:表示当前字段的值要符合一个正则表达式。如果不能匹配,则报一个验证错误。ErrorMessage的内容就是验证错误的具体信息。
/// </summary>
[Display(Name = "邮箱")] //也可以写成 [DisplayName("邮箱")]
[Required(AllowEmptyStrings = false, ErrorMessage = "邮箱不能为空")]
//[DataType(DataType.EmailAddress, ErrorMessage = "xxxxxxxx")] //其实也可以用以下的正则表达式来验证邮箱
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9]+\.[A-Za-z]{2,4}", ErrorMessage = "{0}的格式不正确")]
public string Email { get; set; } //邮箱 [DisplayName("请再次确认邮箱")]
[System.ComponentModel.DataAnnotations.Compare("Email", ErrorMessage = "两次输入的邮箱不一致")]
//[DataType(DataType.EmailAddress, ErrorMessage = "请输入有效的邮箱地址")]
public string TEmail { get; set; } //再次输入邮箱 [DisplayName("身份证")]
[RegularExpression(@"\d{17}[\d|x]|\d{15}", ErrorMessage = "身份证号码格式错误")]
public string IdentityNo { get; set; } //身份证号 /// <summary>
/// Range:用来指定数值类型值的最小值和最大值。括号里的第一个数的“最小值”第二个参数是“最大值”
/// </summary>
[DisplayName("年龄")]
[Required(AllowEmptyStrings = false, ErrorMessage = "年龄不能为空")]
[Range(, , ErrorMessage = "您输入的年龄不符合规范,年龄应该在{1}-{2}之间")]
public int Age { get; set; } //年龄 /// <summary>
/// DisplayFormat:用来处理属性的各种格式化选项。当属性包含空值时,可以提供可选的显示文本,也可以为包含标记的属性关闭HTML编码,还可以为运行时指定一个应用于属性值的格式化字符串。
/// </summary>
[DisplayName("金额")]
[DataType(DataType.Currency)] //Currency:表示货币值
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
[Required(ErrorMessage = "金额不能为空")]
[Range(typeof(decimal), "20.0", "30.0", ErrorMessage = "金额在{1}和{2}之间")]
public decimal Money { get; set; } //金额 [Display(Name = "手机号码")]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)", ErrorMessage = "格式不正确")]
public int Phome { get; set; } [Display(Name = "生日")]
//[DisplayFormat(ApplyFormatInEditMode=true,DataFormatString="yyyy/MM/dd")]
[Required] //当前字段的值不能为空
public DateTime Birthday { get; set; } //生日 [Display(Name = "备注")]
[DataType(DataType.MultilineText)] //当前字段是个多行文本
public string Remarks { get; set; } //备注 }
}

View (Index)

 @model Model.UserInfo
@{
ViewBag.Title = "主页";
}
<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>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true) <fieldset>
<legend>UserInfo</legend> <div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div> <div class="editor-label">
@Html.LabelFor(model => model.UserPassword)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserPassword)
@Html.ValidationMessageFor(model => model.UserPassword)
</div> <div class="editor-label">
@Html.LabelFor(model => model.TUserPassword)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TUserPassword)
@Html.ValidationMessageFor(model => model.TUserPassword)
</div> <div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div> <div class="editor-label">
@Html.LabelFor(model => model.TEmail)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TEmail)
@Html.ValidationMessageFor(model => model.TEmail)
</div> <div class="editor-label">
@Html.LabelFor(model => model.IdentityNo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.IdentityNo)
@Html.ValidationMessageFor(model => model.IdentityNo)
</div> <div class="editor-label">
@Html.LabelFor(model => model.Age)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</div> <div class="editor-label">
@Html.LabelFor(model => model.Money)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Money)
@Html.ValidationMessageFor(model => model.Money)
</div> <div class="editor-label">
@Html.LabelFor(model => model.Phome)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Phome)
@Html.ValidationMessageFor(model => model.Phome)
</div> <div class="editor-label">
@Html.LabelFor(model => model.Birthday)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Birthday)
@Html.ValidationMessageFor(model => model.Birthday)
</div> <div class="editor-label">
@Html.LabelFor(model => model.Remarks)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Remarks)
@Html.ValidationMessageFor(model => model.Remarks)
</div> <p>
<input type="submit" value="Create" />
</p>
</fieldset>
} <div>
@Html.ActionLink("Back to List", "Index")
</div>

 最终效果

MVC 数据验证收集代码的更多相关文章

  1. MVC 数据验证

    MVC 数据验证 前一篇说了MVC数据验证的例子,这次来详细说说各种各样的验证注解.System.ComponentModel.DataAnnotations 一.基础特性 一.Required 必填 ...

  2. MVC 数据验证[转]

    前一篇说了MVC数据验证的例子,这次来详细说说各种各样的验证注解. 一.基础特性 一.Required 必填选项,当提交的表单缺少该值就引发验证错误. 二.StringLength 指定允许的长度 指 ...

  3. MVC 数据验证【转】

    [转自]http://www.cnblogs.com/dozer/archive/2010/04/12/MVC-DataAnnotations.html 作者Dozer 今天在这里给大家介绍一下MVC ...

  4. mvc 数据验证金钱格式decimal格式验证

    mvc 数据验证金钱格式decimal格式验证 首先看下代码 /// <summary> /// 产品单价 /// </summary> [Display(Name = &qu ...

  5. MVC数据验证

    深入浅出 MVC 数据验证 2.0 [附演示源码] 今天在这里给大家介绍一下MVC的数据验证框架. 在1.0版中,很多朋友提出了怎么使用客户端验证,今天找了一些资料,发现了客户端验证的方法. 1.MV ...

  6. MVC数据验证使用小结

    原文:MVC数据验证使用小结 描述:MVC数据验证使用小结 内容:display,Required,stringLength,Remote,compare,RegularExpression 本人最近 ...

  7. Hibernate Validation,Spring mvc 数据验证框架注解

    1.@NotNull:不能为 Null,但是可以为Empty:用在基本数据类型上. @NotNull(message="{state.notnull.valid}", groups ...

  8. MVC数据验证原理及自定义ModelValidatorProvider实现无编译修改验证规则和错误信息

    Asp.net MVC中的提供非常简单易用的数据验证解决方案. 通过System.ComponentModel.DataAnnotations提供的很多的验证规则(Required, StringLe ...

  9. 【转】ASP.NET MVC 数据验证及相关内容

    原文地址:http://www.jb51.net/article/56713.htm 一.数据验证 数据验证的步骤在模型类中添加与验证相关的特性标记在客户端导入与验证相关的js文件和css文件使用与验 ...

随机推荐

  1. MVC 项目 在前台使用DataTable

    1:后台控制器代码 //CreateTestOutputDataHeader生成一个测试DataTable public ActionResult UseDataTable() { DataTable ...

  2. 华为OJ—字符串排序(排序,忽略指定字符排序)

    http://career-oj.huawei.com/exam/ShowProblemInfo?id=2168 编写一个程序,将输入字符串中的字符按如下规则排序. 规则1:英文字母从A到Z排列,不区 ...

  3. 后缀为inc的是什么文件?C#中如何包含inc文件?

    在项目Web页面文件中,发现这么一句话: <!-- 页面字符集设置 begin--><!-- #INCLUDE FILE="http://www.cnblogs.com/C ...

  4. Dockpanel的控件加载问题

    1. 正确加载模式:panel.ControlContainer.Controls.Add(control); 如果用panel.Controls.Add(control);则可能出现模块发生位移问题 ...

  5. Visual Studio 2010 更新NuGet Package Manager出错解决办法

    在Visual Studio 2010的扩展管理器中发现NuGet Package Manger有最新版本更新提示,选择更新安装提示以下错误信息: 2013/4/25 1:11:48 - Micros ...

  6. (转)Android如何编程设置APP安装位置(外部存储或内部存储)?

    Beginning with API Level 8, you can allow your application to be installed on the external storage ( ...

  7. 一个ListView中显示不同的item(分组)

    MainActivity: package com.zzw.qqgroup; import java.util.ArrayList; import java.util.HashMap; import ...

  8. js实现选项卡功能

    1.css .liclick{ border: 1px black solid; background: #fff; float: left; width: 80px; height: 35px; l ...

  9. SynchronizationContext一篇

    SynchronizationContext context; 最近写代码用到了这个,特别记录一下. 作用如下: // 摘要: // 提供在各种同步模型中传播同步上下文的基本功能. public cl ...

  10. PIL不能关闭文件的解决方案

    今天写了一个能指定图片尺寸,以及比例 来搜索分类图片的Python脚本.为了读取多个格式的文件的头,采用了Python PIL库. im = PIL.Image.open(imPath) if im的 ...