我只是想知道为什么Range验证属性可以将Type和两个字符串作为参数?这是用于验证字符串对Enum或类似的东西?
我正在尝试做的是找到一种简单的方法来验证一个3字符串,它必须存在于枚举,任何sugestions?
谢谢,亚历克斯.
我有一个使用Entity Framework 4.1的关系,我的外键是自动生成的,外键的名称有下划线:
public class Setor : Entity
{
public long Id { get; set; }
public string Nome { get; set; }
public virtual Secretaria Secretaria { get; set; }
}
public class Secretaria : Entity
{
public long Id { get; set; }
public string Nome { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这在表Setor中生成了名为:Secretaria_Id的外键
我想删除这个下划线:SecretariaId
有没有办法做到这一点?我更喜欢使用DataAnnotations.
是否有可能增加更多Data Anootation成员,例如Range,Required,...到Entity Framework或Linq to SQL自动生成的类?
我想为我的类使用数据注释验证
谢谢
编辑1)
我为Northwind数据库创建一个实体框架模型并添加Product类.代码的一部分是这样的:
[EdmEntityTypeAttribute(NamespaceName="NorthwindModel", Name="Product")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class Product : EntityObject
{
#region Factory Method
/// <summary>
/// Create a new Product object.
/// </summary>
/// <param name="productID">Initial value of the ProductID property.</param>
/// <param name="productName">Initial value of the ProductName property.</param>
/// <param name="discontinued">Initial value of the Discontinued property.</param>
public static Product CreateProduct(global::System.Int32 productID, global::System.String productName, global::System.Boolean discontinued)
{
Product …Run Code Online (Sandbox Code Playgroud) 我有不同的人应该看到不同名称的字段.
例如,假设我有以下用户类型:
public enum UserType {Expert, Normal, Guest}
Run Code Online (Sandbox Code Playgroud)
我实现了一个IMetadataAware属性:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class DisplayForUserTypeAttribute : Attribute, IMetadataAware
{
private readonly UserType _userType;
public DisplayForUserTypeAttribute(UserType userType)
{
_userType = userType;
}
public string Name { get; set; }
public void OnMetadataCreated(ModelMetadata metadata)
{
if (CurrentContext.UserType != _userType)
return;
metadata.DisplayName = Name;
}
}
Run Code Online (Sandbox Code Playgroud)
我的想法是,我可以根据需要覆盖其他值,但是当我不这样做时,可以使用默认值.例如:
public class Model
{
[Display(Name = "Age")]
[DisplayForUserType(UserType.Guest, Name = "Age (in years, round down)")]
public string Age { get; set; }
[Display(Name …Run Code Online (Sandbox Code Playgroud) 虽然我们主要使用流畅的配置来实现代码优先的POCO,但我们发现对表名,PK等使用数据注释很有用,因为它使得没有引用的非EF组件更容易使用到ObjectContext与这些实体进行交互.
根据我们的经验,似乎两种配置样式可以自由混合,流畅的配置覆盖DataAnnotations.这记录在哪里?这种混合配置有任何风险吗?
我们目前正在使用EF 4.3.1
c# entity-framework data-annotations ef-code-first entity-framework-4.3
我有一个正则表达式,我正在试图找出我正在使用的.NET数据注释验证模型.目前它看起来像这样
[RegularExpression("^((?!City)[a-zA-Z '])+$", ErrorMessage = "City is required and must be properly formatted.")]
Run Code Online (Sandbox Code Playgroud)
这个正则表达式的前半部分是占城市,下半部分是确保其唯一的字母和空格.
输入文本框的默认值为City,因此只有"City"无法通过验证.我似乎无法获得该正则表达式的第一部分以使这些案例通过.
Cityville
City City
Jersey City
Arizona City
Run Code Online (Sandbox Code Playgroud)
但是这个案子没有通过:
City
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
*编辑* 这个做了诀窍:
^((?!^ City $)[a-zA-Z'])+ $
我更新了所有验证以包含此内容.以防你想看看我在做什么.这里是我正在使用的整个验证数据模型的代码.(用正确的正则表达式更新)
public class FormModel {
[Required(ErrorMessage = "First Name is a Required field.")]
[DataType(DataType.Text)]
[Display(Order = 1, Name = "FirstName")]
[RegularExpression("^((?!^First Name$)[a-zA-Z '])+$", ErrorMessage = "First name is required and must be properly formatted.")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last Name is a …Run Code Online (Sandbox Code Playgroud) 以下是我的MVC模型中的一个属性.
[Display(Name = "Event ID")]
[MaxLength(8, ErrorMessage = "Event ID can be of maximum 8 characters long")]
[Required(ErrorMessage="Event ID must be entered")]
public Nullable<int> ID_EVENTO { get; set; }
Run Code Online (Sandbox Code Playgroud)
我已使用View绑定模型,当我尝试单击"提交"按钮时,它会给出以下运行时错误 -
无法将"System.Int32"类型的对象强制转换为"System.Array"类型
然而,如果我删除"MaxLength"属性,它就会开始工作.
这可能是什么问题?
我在MVC4工作,想要使用Uppercase属性定义模型.我们的想法是,大写属性的存在会导致模型值在到达服务器时转换为大写.
目前我在模型中有以下代码:
[Required]
[Display(Name="Account Code")]
[StringValidation(RegExValidation.AccountCode, Uppercase=true)]
public string Account
{
get { return _account; }
set
{
if (value != null)
_account = value.ToUpper();
}
}
Run Code Online (Sandbox Code Playgroud)
但我真正喜欢的是:
[Required]
[Display(Name="Account Code")]
[StringValidation(RegExValidation.AccountCode)]
[Uppercase]
public string Account { get; set; }
Run Code Online (Sandbox Code Playgroud)
我认为我可能需要创建一个Uppercase属性,ValidationAttribute以确保在模型到达服务器时触发它.但这似乎有点不对,因为我并没有真正验证数据.有没有更好的办法?
另外,有没有办法确保属性的调用顺序?我真的想在自定义StringValidation属性触发之前将数据转换为大写,因为这会检查正则表达式模式中文本的大小写.
要为此添加一些背景知识,我希望减少添加代码以大写数据的需要.涅(nirvana)将是一个单一属性,它可以在模型绑定或验证阶段更新进入服务器的数据.然后可以在StringValidation属性中引用此属性以修改其检查中使用的RegEx值.然后我也可以在自定义TextBoxFor帮助器方法中查找此属性,这样我就可以添加text-transform: uppercase它,使其在客户端看起来正确.
有没有人有任何想法?
更新建议后,我仍然收到错误
和.SingleOrDefault()或试过FirstOrDefault()

我需要检索StringLength注释值,这是我的代码,但我收到以下错误.
我试图从这里实现几乎相同的代码,但得到错误:
序列不包含任何元素
public static class DataAnnotation
{
public static int GetMaxLengthFromStringLengthAttribute(Type modelClass, string propertyName)
{
int maxLength = 0;
var attribute = modelClass.GetProperties()
.Where(p => p.Name == propertyName)
.Single()
.GetCustomAttributes(typeof(StringLengthAttribute), true)
.Single() as StringLengthAttribute;
if (attribute != null)
maxLength = attribute.MaximumLength;
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
//调用:
int length = DataAnnotation.GetMaxLengthFromStringLengthAttribute(typeof(EmployeeViewModel), "Name");
public class EmployeeViewModel
{
[StringLength(20, ErrorMessage = "Name cannot be longer than 20 characters.")]
public string Name{ get; set; …Run Code Online (Sandbox Code Playgroud) 我有这个枚举(Notebook.cs):
public enum Notebook : byte
{
[Display(Name = "Notebook HP")]
NotebookHP,
[Display(Name = "Notebook Dell")]
NotebookDell
}
Run Code Online (Sandbox Code Playgroud)
我的班级(TIDepartment.cs)中的这个属性:
public Notebook Notebook { get; set; }
Run Code Online (Sandbox Code Playgroud)
它工作得很好,我只有一个"问题":
我创建了一个EnumDDLFor,它显示我在DisplayAttribute中设置的名称,带有空格,但是对象在DisplayAttribute中没有收到该名称,收到Enum名称(正确),所以我的问题是:
有没有办法接收带有我在DisplayAttribute中配置的空格的名称?
data-annotations ×10
c# ×7
asp.net-mvc ×5
.net ×1
c#-4.0 ×1
enums ×1
linq-to-sql ×1
maxlength ×1
regex ×1