MVC4資料註解和驗證

2022-02-10 10:49:21 字數 1953 閱讀 8486

model中的驗證註解特性:

public class stuinfo

[display(name = "姓名")] //設定要顯示的欄位名

[required(errormessage = "您需要填寫")] //設定為必須字段 已經錯誤提示

[stringlength(50, minimumlength = 3)] //設定最大長度和最小長度

public string name

[display(name = "年齡")]

[range(1, 150, errormessage = "年齡填寫不正確!")] //設定 值範圍

public int age

[display(name = "身高")]

[range(typeof(decimal),"50.00", "250.00",errormessage = "身高超出指定範圍")]

public decimal height

[display(name = "生日")]

[datatype(datatype.date,errormessage = "格式不正確")] //設定資料型別以及錯誤提示

public datetime birthday

[display(name = "**")]

[remote("checkphone","stuinfo", errormessage = "已被註冊")] //在指定的conteroller中的通道(route)中驗證資料

public string phone

[display(name = "位址")]

[datatype(datatype.multilinetext)]

public string address

[display(name = "電子郵箱")]

[regularexpression(@"(\w)+(\.\w+)*@(\w)+((\.\w+)+)",errormessage = "格式不正確")] //正則驗證

public string email

[display(name = "再次輸入電子郵箱")]

[compare("email",errormessage = "兩次輸入不一致")] //設定比較兩個欄位的值

public string emailconfirm

[display(name = "密碼")]

[datatype(datatype.password)]

public string password

[display(name = "備用電子郵箱")]

[datatype(datatype.emailaddress,errormessage = "格式不正確")]

public string email_b

}

遠端驗證的通道:

public jsonresult checkphone(string phone)

效果: 

正常下的網頁:

必須字段驗證效果:

值範圍、值型別驗證效果:

正則驗證和比較字段值效果:

資料型別驗證效果:

MVC4學習筆記(三) 資料驗證設計

看字面也許不能直觀的理解,好吧,其實講的就是 密碼必須大於6位數!呀之類的項 屬性 加一些控制條件,因為總有一些資料是有特定格式的。using system.web.mvc using system.componentmodel using system.componentmodel.dataann...

MVC4學習筆記(一) 認識MVC

mvc就是為了快速開發出乙個軟體專案,有多快了?你甚至可以在5分鐘內開發出乙個五臟俱全的軟體系統,一點也不假。mvc model view controller,模型 檢視 控制器模式 用於表示一種軟體架構模式。它把軟體系統分為三個基本部分 模型 model 檢視 view 和控制器 control...

MVC4程式執行報錯

近期了解mvc4的時候弄了乙個簡單的小工程,使用entity framework作為model,f5啟動除錯執行的時候沒有問題,但是發布到iis之後訪問就報錯 錯誤資訊如下 很奇怪,搜尋了一下,找到乙個老外的帖子,戳這裡 解決方案如下 首先檢查ef相關的程式集是否被引用,主要有兩個 entityfr...