Tag: data annotations

MVC 4razor数据注释ReadOnly

ReadOnly属性似乎不在MVC 4中。可编辑(false)属性不能按照我希望的方式工作。 有类似的东西有效吗? 如果没有,那么我如何创建自己的ReadOnly属性,如下所示: public class aModel { [ReadOnly(true)] or just [ReadOnly] string aProperty {get; set;} } 所以我可以这样说: @Html.TextBoxFor(x=> x.aProperty) 而不是这(它确实有效): @Html.TextBoxFor(x=> x.aProperty , new { @readonly=”readonly”}) 或者这(它确实有效,但未提交值): @Html.TextBoxFor(x=> x.aProperty , new { disabled=”disabled”}) http://view.jquerymobile.com/1.3.2/dist/demos/widgets/forms/form-disabled.html 这样的事可能吗? https://stackoverflow.com/a/11702643/1339704 注意: [可编辑(假)]无效

未找到System.ComponentModel.DataAnnotations.Schema

我遇到涉及System.ComponentModel.DataAnnotations.Schema命名空间的Visual Studio 2012中的问题。 它告诉我,ForeignKeyAttribute无法解析,过去的解决方案是添加下面注释掉的using语句。 VS2012无法像VS2010那样解析Schema名称空间。 在最近的.Net版本中是否有任何改变会导致这个问题? 如果是这样,我该如何解决它们? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel.DataAnnotations; // using System.ComponentModel.DataAnnotations.Schema; namespace Data { public class AffiliateUser { [Key, ForeignKey(“User”)] public int UserId { get; set; } [StringLength(50)] public string AffiliateUserKey { get; set; } public Guid ApiKey { get; set; } public string PasswordHash { […]

派生的RequiredAttribute不起作用

我正在尝试实现自己的RequiredAttribute,我在其中调用自定义资源处理程序: public class LocalizedValidationAttributes { public class LocalizedRequiredAttribute : RequiredAttribute { private String _resourceString = String.Empty; public new String ErrorMessage { get { return _resourceString; } set { _resourceString = GetMessageFromResource(value); } } } private static String GetMessageFromResource(String resourceTag) { return ResourceManager.Current.GetResourceString(resourceTag); } } 我用以下方式称呼它: [LocalizedValidationAttributes.LocalizedRequiredAttribute(ErrorMessage = “test”)] public String Text { get; set; } […]

TimeSpan上的Range和DisplayFormat属性

我正在构建一个计划屏幕,需要显示一个时间字段供用户输入计划的时间。 我不确定这是否是最好的选择,但我正在使用TimeSpan进行该领域。 要validation输入,我想使用Range属性和DisplayFormat属性。 当我调试并输入一个看似有效的值时,Range属性表示超出范围错误。 谁能看到我做错了什么? TimeSpan是否适合此用途? 任何帮助是极大的赞赏。 型号类: public class Schedule { public Schedule() { this.ScheduleTime = new TimeSpan(0, 0, 0); } /// /// The time of day for the schedule to run /// [Required, DataType(System.ComponentModel.DataAnnotations.DataType.Time), Display(Name = “Schedule Time”, Description = “Number of Hours and Minutes after Midnight Central Timezone”), DisplayFormat(DataFormatString = @”{0:hh\:mm\:ss}”, ApplyFormatInEditMode […]

对于string 或List ,DataAnnotations MaxLength和StringLength?

我试图弄清楚是否有一种正确的方法来实现DataAnnotations: 有一个数组或字符串列表,其中数组或列表中的最大元素数是2个项目,每个字符串可能只有255个字符长。 这会工作: [MaxLength(2)] [StringLength(255)] public string[] StreetAddress { get; set; } 我宁愿不必创建一个新类只是为了保存一个字符串Value属性来将每个字符串约束为255个字符。

DataAnnotation属性伙伴类奇怪 – ASP.NET MVC

鉴于这个POCO类是由EntityFramework T4模板自动生成的(没有也无法以任何方式手动编辑): public partial class Customer { [Required] [StringLength(20, ErrorMessage = “Customer Number – Please enter no more than 20 characters.”)] [DisplayName(“Customer Number”)] public virtual string CustomerNumber { get;set; } [Required] [StringLength(10, ErrorMessage = “ACNumber – Please enter no more than 10 characters.”)] [DisplayName(“ACNumber”)] public virtual string ACNumber{ get;set; } } 请注意,“ACNumber”是一个命名错误的数据库字段,因此自动生成器无法生成正确的显示名称和错误消息,该消息应为“帐号”。 所以我们手动创建这个伙伴类来添加无法自动生成的自定义属性: [MetadataType(typeof(CustomerAnnotations))] public […]

是否可以在ViewModel中重用DataAnnotations?

在我的MVC应用程序中,我在域模型中定义了DataAnnotations。 尽管在使用域模型时可以检索DataAnnotations属性作为Display等,但在ViewModel上使用相同属性并使用此ViewModel时,无法检索它们。 我认为再次在ViewModel中定义DataAnnotations似乎并不好。 那么,我应该遵循哪种方式? 领域模型: public class Issue { [Key] public int ID { get; set; } [Required(ErrorMessage = “Required”)] [Display(Name = “Project Number”)] public int ProjectID { get; set; } [Required(ErrorMessage = “Required”)] [Display(Name = “Issue Definition”)] public string Description { get; set; } //… removed for brevity //Navigation Properties: public virtual ICollection FileAttachments […]

使用自定义validation属性时获取错误消息

我正在使用像这样的CustomValidationAttribute [CustomValidation(typeof(MyValidator),”Validate”,ErrorMessage = “Foo”)] 我的validation器包含此代码 public class MyValidator { public static ValidationResult Validate(TestProperty testProperty, ValidationContext validationContext) { if (string.IsNullOrEmpty(testProperty.Name)) { return new ValidationResult(“”); <– how can I get the error message from the custom validation attribute? } return ValidationResult.Success; } } 那么如何从自定义validation属性中获取错误消息?

使用数据注释创建外键

在下面的代码中,我需要在ParentInfoAddProperties.ParentQuestionAnswersId上设置一个外键constrant,以便它依赖于ParentQuestionAnswers.Id(这是一个主键)。 我试图使用数据注释,但entity framework6想要在我的ParentQuestionAnswers表中创建一个新的外键列,该列引用ParentInfoAddProperties.Id列而不是ParentInfoAddProperties.ParentQuestionAnswersId列。 我不希望Entity Framework创建新的外键列。 如果有人能够解释我应该指定哪些数据注释或(如果需要)流畅的映射来实现所需的外键实例,我将不胜感激。 提前致谢。 namespace Project.Domain.Entities { public class ParentQuestionAnswers { public ParentQuestionAnswers() { ParentInfoAddProperties = new ParentInfoAddProperties(); } [Required] public int Id { get; set; } [Required] public int UserId { get; set; } public ParentInfoAddProperties ParentInfoAddProperties { get; set; } } public class ParentInfoAddProperties { [Required] public int Id { […]

DataType与UiHint

我一直在使用mvc2一段时间,当我需要设置模板时,我使用DataType属性 [DataType(“DropDown”)] public int Field { get; set; } 我看到其他人使用UiHint来达到相同的效果 [UiHint(“DropDown”)] public int Field { get; set; } 使用这两个属性有什么区别? 我应该正常使用哪个属性,还是针对不同的任务?