我有一些代码枚举一个对象,并根据其ValidationAttribute记录它所有的错误.
当它找到它们时,我希望创建一个名为RuleViolations的自定义类的集合.RuleViolation类如下所示:
public string Message { get; set; }
public LambdaExpression Property { get; set; }
Run Code Online (Sandbox Code Playgroud)
属性是lambda表达式,因此该属性不必是字符串.当我手动添加错误但是我不确定如何指定LambdaExpression时,我所拥有的是属性的PropertyDescriptor对象.
有谁知道怎么样?
我的类中有一个int属性,并想验证用户是否输入了一个字符串.如何使用数据注释来做到这一点?当我传递一个非整数值时,我得到一个像这样的excpetion:
The value 'asdasd' is not valid for property.
Run Code Online (Sandbox Code Playgroud)
例如,使用此验证属性:
[Range(0, Int32.MaxValue, ErrorMessage="Invalid Number")]
public int? Number { get; set; }
Run Code Online (Sandbox Code Playgroud)
并在使用该模型的字段中输入'aaa'我已经获得了此消息的例外情况:
The value 'aaa' is not valid for Number.
Run Code Online (Sandbox Code Playgroud)
而不是"无效的数字"消息.
有任何想法吗?
我已经把
[Range(0, Int32.MaxValue, ErrorMessage="Invalid Number")]
public int? Number { get; set; }
Run Code Online (Sandbox Code Playgroud)
但我从这个消息中得到了这个消息
The value 'aaa' is not valid for Number.
Run Code Online (Sandbox Code Playgroud) 如何在模型上运行所有DataAnnotation验证?
我正在从代码构建一个模型实例,我没有没有模型状态绑定或任何东西.我只想对它进行所有验证......我正在使用EF CodeFirst.
public class Category
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
}
cat = new Category();
if (cat.IsValid()) { /* blah */ } // i want something like this
Run Code Online (Sandbox Code Playgroud)
我知道这可能是一个愚蠢的问题,但我似乎无法在任何地方找到答案..
我正在使用如下验证.
[Required(ErrorMessage = "Please provide 10 digit mobile number without spaces and without country code (+91)")]
[Integer(ErrorMessage = "Please provide 10 digit mobile number without spaces and without country code (+91)")]
[Range(1000000000, 9999999999, ErrorMessage = "10 digit mobile number only without spaces and without country code (+91)")]
[Display(Name = "Mobile Number")]
public int MobileNo { get; set; }
Run Code Online (Sandbox Code Playgroud)
验证总是失败The value '9999999998' is invalid..难道我做错了什么?
我们在域模型中使用了自定义LocalizedString类型.我们想用验证属性来装饰属性MaxLength.为此,我们添加了隐式运算符以启用此属性所需的强制转换.
奇怪的是,运算符似乎永远不会被调用,并且在属性IsValid方法中抛出了InvalidCastException .在我们自己的项目中执行此演员表.
在这个系统clr ngen'ed属性中有没有特殊的强制转换行为编译器magix?
// Custom type
public class LocalizedString
{
public string Value
{
get { return string.Empty; }
}
public static implicit operator Array(LocalizedString localizedString)
{
if (localizedString.Value == null)
{
return new char[0];
}
return localizedString.Value.ToCharArray();
}
}
// Type: System.ComponentModel.DataAnnotations.MaxLengthAttribute
// Assembly: System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.ComponentModel.DataAnnotations.dll
public override bool IsValid(object value)
{
this.EnsureLegalLengths();
if (value == null)
{
return true;
}
else
{
string str = value …Run Code Online (Sandbox Code Playgroud) 我有一个asp mvc 5项目,并使用代码优先和数据迁移创建了数据库.
这是我目前的产品POCO类
public class Product
{
[Key]
public int ProductId { get; set; }
[Required(ErrorMessage = "Please add a product name.")]
[StringLength(50)]
public string Name { get; set; }
public string Code { get; set; }
[DataType(DataType.MultilineText)]
public string Description { get; set; }
public string Features { get; set; }
public decimal Price { get; set; }
public bool? Promotion { get; set; }
[Display(Name = "Category")]
public int CategoryId { get; set; }
public virtual Category …Run Code Online (Sandbox Code Playgroud) 正如标题所说,我正在寻找一个不允许用户使用字符提交名称的正则表达式:(冒号),; (分号),/(正斜杠),\(反斜杠),*(星号)和.(点)
[Required]
public string Name { get; set; }
Run Code Online (Sandbox Code Playgroud) 基于本文:带有数据注释的手动验证
我写这段代码:
public class Person04
{
[CustomValidation(typeof(AWValidation), "ValidateSalesAmount")]
public int SalesAmout { get; set; }
[DataType(DataType.EmailAddress, ErrorMessage = "Invalid E-mail")]
public string EmailAddress { get; set; }
[Range(0, 99, ErrorMessage = "Age should be in range 0 to 99")]
public int Age { get; set; }
[Required(ErrorMessage="Name is required")]
public string Name { get; set; }
[StringLength(10, ErrorMessage = "Invalid Last Name")]
public string LastName { get; set; }
}
public class AWValidation
{
public static ValidationResult ValidateSalesAmount(int salesAmount) …Run Code Online (Sandbox Code Playgroud) [Table("LegalEntity")]
[ModelMetadataType(typeof(LegalEntityMeta))]
public class LegalEntity : Entity<long>
{
}
public class LegalEntityMeta
{
[JsonProperty(PropertyName = "LegalEntityId")]
public long Id { get; set; }
[JsonProperty(PropertyName = "LegalEntityName")]
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在Startup.cs中...
services
.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
})
.AddAutoMapper(typeof(Startup))
.AddMvcCore()
.AddJsonFormatters()
.AddApiExplorer();
Run Code Online (Sandbox Code Playgroud)
我的期望是看到具有属性legalEntityId和legalEntityName的json,但生成的json具有id和name作为属性。有人可以帮助我如何更改json属性吗?谢谢阿南德
使用DataTypeAttribute和传入值DataType.Phone以及继承和自动设置的PhoneAttribute之间的区别是什么?DataTypeDataType.Phone
这两个班级有什么区别吗?
class Person {
[DataType(DataType.PhoneNumber)]
public string PhoneNumber {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
class Person {
[Phone]
public string PhoneNumber {get;set;}
}
Run Code Online (Sandbox Code Playgroud) data-annotations ×10
c# ×6
asp.net-mvc ×4
validation ×3
.net ×1
c#-4.0 ×1
code-first ×1
json.net ×1
model ×1