建立一個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: RegularExpressionAttribute
10 {
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視圖即可