実際はErrorMessageプロパティ使うよな〜と思いつつ実験。
簡単にRequiredAttributeで。
using System.ComponentModel.DataAnnotations; namespace MyApplication { public class Person { private string _name; [Required( ErrorMessageResourceName = "ErrorMessage", ErrorMessageResourceType = typeof(MyApplication.MyResource))] public string Name { get { return _name; } set { Validator.ValidateProperty( value, new ValidationContext(this, null, null) { MemberName = "Name" }); _name = value; } } } }
なんか例外出るし。
リソース型 'MyApplication.MyResource' には 'ErrorMessage' というアクセス可能な static プロパティは含まれていません。
なるほどね。
http://forums.asp.net/t/1433699.aspx
リソースはデフォルトはInternalだからPublicにしとけってさ。
おまけ
Imports System.ComponentModel.DataAnnotations Public Class Person Private _name As String <Required( _ ErrorMessageResourceName:="ErrorMessage", _ ErrorMessageResourceType:=GetType(My.Resources.MyResource))> _ Public Property Name() As String Get Return _name End Get Set(ByVal value As String) Validator.ValidateProperty( _ value, New ValidationContext(Me, Nothing, Nothing) With {.MemberName = "Name"}) _name = value End Set End Property End Class