Tag: 数据注释

使用WPF和entity framework的DataAnnotationsvalidation数据?

有什么方法来validation在WPF和entity framework中使用DataAnnotations?

DataAnnotation为必需属性

首先它有效,但今天它失败了! 这是我如何定义date属性: [Display(Name = "Date")] [Required(ErrorMessage = "Date of Submission is required.")] [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)] [DataType(DataType.Date)] public DateTime TripDate { get; set; } 它一直在工作。 但今天,当我调用相同的ApiController行动: [HttpPost] public HttpResponseMessage SaveNewReport(TripLeaderReportInputModel model) Firebug报道: ExceptionMessage: "Property 'TripDate' on type 'Whitewater.ViewModels.Report.TripLeaderReportInputModel' is invalid. Value-typed properties marked as [Required] must also be marked with [DataMember(IsRequired=true)] to be […]

Validator.TryValidateObject不validationRangeAttribute

鉴于以下目的, public class Question { [Required] public string QuestionText { get; set; } [Range(1, 5)] public int Difficulty { get; set; } } 使用以下validation码 ICollection<ValidationResult> results = new List<ValidationResult>(); Question question = new Question(); ValidationContext ctx = new ValidationContext(question, null, null); Validator.TryValidateObject(question, ctx, results); // results.Length = 1 为什么Range属性在Required中没有创buildvalidation错误(值显然是0)?

使用AngularJS的ASP.NET MVCvalidation表单

我在MVC 4和AngularJS(+ twitter引导)项目。 我通常在我的MVC项目中使用“jQuery.Validate”,“DataAnnotations”和“Razor”。 然后我在我的web.config中启用这些键来validation客户机上模型的属性: <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 例如,如果我在我的模型中有这个: [Required] [Display(Name = "Your name")] public string Name { get; set; } 有了这个Cshtml: @Html.LabelFor(model => model.Name) @Html.TextBoxFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) html结果会: <label for="Name">Your name</label> <input data-val="true" data-val-required="The field Your name is required." id="Name" name="Name" type="text" value="" /> <span class="field-validation-valid" […]

数据注释validation确认密码

我的用户模型有这些数据注释来validationinput字段: [Required(ErrorMessage = "Username is required")] [StringLength(16, ErrorMessage = "Must be between 3 and 16 characters", MinimumLength = 3)] public string Username { get; set; } [Required(ErrorMessage = "Email is required"] [StringLength(16, ErrorMessage = "Must be between 5 and 50 characters", MinimumLength = 5)] [RegularExpression("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$", ErrorMessage = "Must be a valid email")] public string Email […]

以强types的方式获取属性的属性

美好的一天! 我有这样的方法来获得[DisplayName]属性的值(直接附加或使用[MetadataType]属性)。 在极less数情况下,我需要在控制器代码中获得[DisplayName] 。 public static class MetaDataHelper { public static string GetDisplayName(Type dataType, string fieldName) { // First look into attributes on a type and it's parents DisplayNameAttribute attr; attr = (DisplayNameAttribute)dataType.GetProperty(fieldName).GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault(); // Look for [MetadataType] attribute in type hierarchy // http://stackoverflow.com/questions/1910532/attribute-isdefined-doesnt-see-attributes-applied-with-metadatatype-class if (attr == null) { MetadataTypeAttribute metadataType = (MetadataTypeAttribute)dataType.GetCustomAttributes(typeof(MetadataTypeAttribute), true).FirstOrDefault(); if […]

MVC中UIHint属性的用法是什么

任何人都可以请解释我什么是在MVC中使用UIHint属性。 我们为什么需要这个。 何时以及如何使用。 谢谢

DataAnnotations:recursionvalidation整个对象图

我有一个洒有DataAnnotation属性的对象图,其中对象的某些属性是本身具有validation属性的类,等等。 在以下情况下: public class Employee { [Required] public string Name { get; set; } [Required] public Address Address { get; set; } } public class Address { [Required] public string Line1 { get; set; } public string Line2 { get; set; } [Required] public string Town { get; set; } [Required] public string PostalCode { […]

如何使用DataAnnotations在ASP.NET MVC 2中处理Booleans / CheckBoxes?

我有一个像这样的视图模型: public class SignUpViewModel { [Required(ErrorMessage = "Bitte lesen und akzeptieren Sie die AGB.")] [DisplayName("Ich habe die AGB gelesen und akzeptiere diese.")] public bool AgreesWithTerms { get; set; } } 视图标记代码: <%= Html.CheckBoxFor(m => m.AgreesWithTerms) %> <%= Html.LabelFor(m => m.AgreesWithTerms)%> 结果: 没有validation执行。 到目前为止没关系,因为bool是一个值types,从不为空。 但即使我使AgreesWithTerms为空,它也不会工作,因为编译器喊 “模板只能用于字段访问,属性访问,单维数组索引或单参数自定义索引器expression式。 那么,处理这个问题的正确方法是什么呢?

整数值的必需属性

我有一个Id属性的视图模型 [Required] public int Id { get; set; } 但我认为这个属性只适用于string属性。 如果没有设置Id,Id值为0,模型有效。 我如何执行,如果没有设置一个int属性的值,模型将是无效的?