ASP.NET MVC 的註解

來源:互聯網
上載者:User

標籤:io   os   ar   使用   for   sp   on   cti   bs   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;


namespace 註解的應用.Models
{
public class User
{
[HiddenInput(DisplayValue = false)]
//[ScaffoldColumn(false)]
[Display(Name="編號")]
public int Id { get; set; }

[Display(Name="使用者名稱")]
//[StringLength(10)]
//[Common.MyStringLength(10,ErrorMessage="{0}字數太多了!")]
//[Common.MyStringLength(5)]
[Common.MyStringLength(5,ErrorMessage="{0}字元太少了")]
[Required(ErrorMessage="請輸入使用者名稱")]
[Remote("CheckValid", "Home", AdditionalFields = "UserName,Pwd",ErrorMessage="該使用者已存在,請重新輸入")]
public string UserName { get; set; }


[DataType(DataType.Password)]
[Display(Name="密碼")]
[System.ComponentModel.DataAnnotations.Compare("UserName",ErrorMessage="密碼不一致")]
public string Pwd { get; set; }

[DisplayFormat(DataFormatString="{0:C}",ApplyFormatInEditMode=false)]
[Range(10,30)]
public decimal Price { get; set; }

//使用DataType(DataType.EmailAddress,ErrorMessage="請輸入正確的Email地址")自訂的錯誤訊息不會生效
//[DataType(DataType.EmailAddress,ErrorMessage="請輸入正確的Email地址")]
[RegularExpression(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "請輸入正確的Email地址")]
public string Email { get; set; }
}
}

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace 註解的應用.Common
{
/// <summary>
/// 自訂註解類,自訂註解不支援用戶端驗證,在模型繫結時執行IsValid方法執行驗證邏輯,
/// 如果不調用FormatErrorMessage方法進行格式化,傳入格式化的錯誤訊息,則在
/// [Common.MyStringLength(10,ErrorMessage="{0}字數太多了!")]中自訂的訊息不回生效
/// </summary>
public class MyStringLengthAttribute:ValidationAttribute
{
private readonly int MaxLength;
public MyStringLengthAttribute(int maxLength)
{
this.MaxLength = maxLength;
}

//protected override ValidationResult IsValid(object value, ValidationContext validationContext)
//{

// string content = value.ToString();
// if (content.Length > MaxLength)
// {
// //return new ValidationResult("輸入的字元太多了!^_^");

// string errorMessage = FormatErrorMessage(validationContext.DisplayName);
// return new ValidationResult(errorMessage);
// }
// return ValidationResult.Success;
// //return base.IsValid(value, validationContext);
//}

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value != null)
{
if (value.ToString().Length < MaxLength)
{
//return new ValidationResult("請輸入" + MaxLength + "位以上長度的字元!");
string errorMessage = FormatErrorMessage(validationContext.DisplayName + "請輸入" + MaxLength + "位以上長度的字元!");
return new ValidationResult(errorMessage);
}
}
return ValidationResult.Success;
}

}
}

ASP.NET MVC 的註解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.