MVC中的验证规则

前面的博客中曾经提到过ModelBing机制,也在Demo中体现过,在MVC中更吊的是封装了自定义的验证规则。下面来一个Demo来展现一下,看了后,你一定会爱上它的,能让你少写很多JS语句。

1.View层

<span style="font-size:18px;">@*自动绑定实体模型*@
@model MvcApplication1.Models.User

<h2>Login</h2>
<form method="post">
    @*绑定实体显示名称*@
    @Html.LabelFor(user=>user.ID)
    @*绑定实体值*@
    @Html.TextBoxFor(user => user.ID)
    @*验证规则*@
    @Html.ValidationMessageFor(user => user.ID)<br />
    @Html.LabelFor(user=>user.Password)
    @Html.EditorFor(user => user.Password)
    @Html.ValidationMessageFor(user => user.Password)<br />
    <input type="submit" name="提交" />
</form>

</span>

2.Model层

<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace MvcApplication1.Models
{
    public class User
    {
        //必填项
        [Required]
        //界面绑定的名称
       [DisplayName("用户别称")]
        //限制字符的长度
        [StringLength(6,ErrorMessage="您输入的名字太长了")]
        //绑定的类型
        [DataType(DataType.Text)]

        //[Range(555555,999999)]
        public string ID { get; set; }
        [Required]
        [DataType(DataType.Password)]
        [DisplayName("用户密码")]
        public string Password { get; set; }
    }
}</span>

3.Controller

<span style="font-size:18px;"> public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(User user)
        {
            if (user.ID =="Admin" || user.Password == "Admin")
            {
                return Content("登录成功");
            }
            else
            {
                return Content("密码错误");
            }

        }</span>

分析:整体实现的功能很简单,就是把页面传进的值通过在Controller中验证后返回结果,主要的功能就是在Model中引入了System.ComponentModel.DataAnnotations和System.ComponentModel的空间,然后为实体的属性绑定了一些自定的验证功能例如[Required]、 [DisplayName("用户别称")]、 [StringLength(6,ErrorMessage="您输入的名字太长了")]等,当然这两个命名空间中还有很多,有兴趣的可以查一下。

最终在界面上绑定强类型视图的时候,通过反射机制,自动为每个控件绑定实体属性。



时间: 2024-07-29 10:09:25

MVC中的验证规则的相关文章

[Asp.net MVC]Asp.net MVC5系列——在模型中添加验证规则

目录 概述 在模型中添加验证规则 自定义验证规则 伙伴类的使用 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5系列——添加视图 [Asp.net MVC]Asp.net MVC5系列——添加模型 [Asp.net MVC]Asp.net MVC5系列——从控制器访问模型中的数据 [Asp.net MVC]Asp.net MVC5系列——添加数据 概述 上篇文章中介绍了添加数据,在提交表单的数据的时候,我们需

Yii CModel中rules验证规则

array( array(‘username’, ‘required’),  array(‘username’, ‘length’, ‘min’=>3, ‘max’=>12),  array(‘password’, ‘compare’, ‘compareAttribute’=>’password2′, ‘on’=>’register’),  array(‘password’, ‘authenticate’, ‘on’=>’login’), array(‘Price’,’num

MVC中数据验证

http://www.studyofnet.com/news/339.html http://www.cnblogs.com/kissdodog/archive/2013/05/04/3060278.html 本文导读:ASP.NET MVC3中的Model是自验证的,这是通过.NET4的System.ComponentModel.DataAnnotations命名空间完成的. 我们要做的只是给Model类的各属性加上对应的验证标记(Attributes)就可以让MVC3框架帮我们完成验证.下面

Yii CModel中rules验证规则[转]

array( array(‘username’, ‘required’), array(‘username’, ‘length’, ‘min’=>3, ‘max’=>12), array(‘password’, ‘compare’, ‘compareAttribute’=>’password2′, ‘on’=>’register’), array(‘password’, ‘authenticate’, ‘on’=>’login’), array(‘Price’,’numeri

Struts2系列:(21)在Struts中自定义验证规则

1.Struts实现验证的过程 通过对Struts源代码的学习,总结一下Struts如何实现验证. 在struts-default.xml文件中,有validator和workflow两个拦截器. <interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/> <interceptor

MVC中url路由规则

Routing:首先获取视图页面传过来的请求,并接受url路径中的controller和action以及参数数据,根据规则将识别出来的数据传递给某controller中的某个action方法 MapRoute()有6个方法可以重载 方法1:系统提供的默认路由规则格式 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); r

MVC中的数据验证

一  概述 关于数据验证和数据注解,是任何软件系统不可小觑的必要模块,在软件系统中起到举足轻重的作用,主要作用是为了保证数据安全性.防止漏洞注入和网络攻击. 从数据验证的验证方式来说,我们一般分为客户端验证和服务端验证(或者两种方式相结合),本篇文章主要讲解基于ASP.NET MVC框架的数据验证特性和数据注解. 二  数据验证 (一)ASP.NET MVC 内置六大类数据验证特性 在ASP.NET MVC中,验证特性定义在 System.ComponentModel.DataAnnotatio

ASP.NET MVC4 新手入门教程之八 ---8.向模式中添加验证

在这本部分会将验证逻辑添加到Movie模式,和你会确保验证规则执行任何时候用户试图创建或编辑使用该应用程序的一部电影. 保持事物的干练性 ASP.NET MVC 的核心设计信条之一是 DRY(”Don't Repeat Yourself“,不要重复).ASP.NET MVC 鼓励你只有一次,指定的功能或行为,然后让它无处不在应用程序中反映.这减少了需要编写的代码量,并使你写更少错误倾向和易于维护的代码. ASP.NET MVC 和 Entity Framework Code First 中提供的

报错:非介入式客户端验证规则中的验证类型名称必须唯一。下列验证类型出现重复

当在ASP.NET MVC中,针对一个Model进行添加操作的时候,报如下错误: [InvalidOperationException: 非介入式客户端验证规则中的验证类型名称必须唯一.下列验证类型出现重复: range] 原因是第三方验证程序集和MVC固有验证发生名称的冲突. 解决办法:在Model中属性上注释掉第三方程序集提供的验证特性.