博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC学习十:MVC2中自定义校验
阅读量:2385 次
发布时间:2019-05-10

本文共 3525 字,大约阅读时间需要 11 分钟。

新建一个Mvc2的应用程序;

在Models 文件夹下新建一个类EmailAttribute

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.ComponentModel.DataAnnotations; 6  7 namespace MvcTemp.Models 8 { 9     public class EmailAttribute: RegularExpressionAttribute10     {11         public EmailAttribute()12             : base(@"^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$")13         {14         }15     }16 }

重新编译下项目,下面演示如何自定义使用这个标记

在Model文件夹下新建一个类Student

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.ComponentModel.DataAnnotations;using MvcTemp.Models;namespace MvcTemp.Model{    public class Student    {        private int _id;        [Required(ErrorMessage="必填")]        public int Id        {            get { return _id; }            set { _id = value; }        }        private string _name;        [Required(ErrorMessage="必填")]        public string Name        {            get { return _name; }            set { _name = value; }        }        private string _emailAddress;        [Email(ErrorMessage="错误的邮件地址")]        public string EmailAddress        {            get { return _emailAddress; }            set { _emailAddress = value; }        }    }}

重新编译下项目,添加Controllers,

using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Mvc;
  
namespace
MvcTemp.Controllers
{
    
public
class
StudentController : Controller
    
{
        
//
        
// GET: /Student/
  
        
public
ActionResult Index()
        
{
            
return
View();
        
}
  
        
//
        
// GET: /Student/Details/5
  
        
public
ActionResult Details(
int
id)
        
{
            
return
View();
        
}
  
        
//
        
// GET: /Student/Create
  
        
public
ActionResult Create()
        
{
            
return
View();
        
  
        
//
        
// POST: /Student/Create
  
        
[HttpPost]
        
public
ActionResult Create(FormCollection collection)
        
{
            
try
            
{
                
// TODO: Add insert logic here
  
                
return
RedirectToAction(
"Index"
);
            
}
            
catch
            
{
                
return
View();
            
}
        
}
          
        
//
        
// GET: /Student/Edit/5
   
        
public
ActionResult Edit(
int
id)
        
{
            
return
View();
        
}
  
        
//
        
// POST: /Student/Edit/5
  
        
[HttpPost]
        
public
ActionResult Edit(
int
id, FormCollection collection)
        
{
            
try
            
{
                
// TODO: Add update logic here
   
                
return
RedirectToAction(
"Index"
);
            
}
            
catch
            
{
                
return
View();
            
}
        
}
  
        
//
        
// GET: /Student/Delete/5
   
        
public
ActionResult Delete(
int
id)
        
{
            
return
View();
        
}
  
        
//
        
// POST: /Student/Delete/5
  
        
[HttpPost]
        
public
ActionResult Delete(
int
id, FormCollection collection)
        
{
            
try
            
{
                
// TODO: Add delete logic here
   
                
return
RedirectToAction(
"Index"
);
            
}
            
catch
            
{
                
return
View();
            
}
        
}
    
}
}

  在Global.asax页中注册自己定义的标记

View Code
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Web.Routing;using MvcTemp.Models;using System.ComponentModel.DataAnnotations;namespace MvcTemp{    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,    // 请访问 http://go.microsoft.com/?LinkId=9394801    public class MvcApplication : System.Web.HttpApplication    {        public static void RegisterRoutes(RouteCollection routes)        {            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            routes.MapRoute(                "Default", // 路由名称                "{controller}/{action}/{id}", // 带有参数的 URL                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值            );        }        protected void Application_Start()        {            AreaRegistration.RegisterAllAreas();            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(EmailAttribute), typeof(RegularExpressionAttributeAdapter));            RegisterRoutes(RouteTable.Routes);        }    }}

   最后创建Student的Action视图即可

转载地址:http://nljab.baihongyu.com/

你可能感兴趣的文章
Middle-题目37:199. Binary Tree Right Side View
查看>>
linux下载edk2链接文件
查看>>
政府信息化中的人机交互技术应用
查看>>
五步骤扫清项目督导盲区
查看>>
浅谈烟草企业固定资产信息化管理
查看>>
关于物流企业信息化现状的分析
查看>>
函数节流(throttle)与函数去抖(debounce)
查看>>
关于psycopg
查看>>
淘宝API系列
查看>>
关于BRD,MRD,PRD的概念
查看>>
ODOO 新API修饰符
查看>>
VMWARE虚拟机三种网络模式(BRIDGED,NAT,HOST-ONLY)区别详解
查看>>
linux端口号是否被占用
查看>>
Raspberry Pi(树莓派)试用小记
查看>>
nssm命令使用
查看>>
检测PC端和移动端的方法总结(转)
查看>>
使用@property
查看>>
python中@lazy_property
查看>>
python 数据类型转换
查看>>
linux下查看所有用户及所有用户组
查看>>