data-annotations

Is there a way to reuse data annotations?

老子叫甜甜 提交于 2019-12-06 07:23:04
问题 Is there a way to implement the idea of a data domains (at a property level) inside of a class that is used as a model in a view in ASP.Net MVC 4? Consider this code: public class LoginProfileModel { [DisplayName("Login ID")] [Required(ErrorMessage = "Login ID is required.")] public string LogonID { get; set; } [DisplayName("Password")] [Required(ErrorMessage = "Password cannot be blank.")] [StringLength(20, MinimumLength = 3)] [DataType(DataType.Password)] public string Password { get; set;

EF CodeFirst: Rails-style created and modified columns

核能气质少年 提交于 2019-12-06 07:03:29
问题 Using Entity Framework CodeFirst, how do I create a created datetime column that gets populated with the current timestamp everytime a record is inserted for that table, and a modified datetime column that has a timestamp generated evertime a row is updated? Rails does this by default and I was hoping the EF generated database would have this as well, but it doesn't. Is this something that can be done with data annotations? If so, how? Thanks! 回答1: It is not supported in EF. EF will not

DataAnnotation attributes buddy class strangeness - ASP.NET MVC

∥☆過路亽.° 提交于 2019-12-06 05:39:48
问题 Given this POCO class that was automatically generated by an EntityFramework T4 template (has not and can not be manually edited in any way): 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

Has the behavior for DataAnnotations in asp.net mvc 3 changed?

只谈情不闲聊 提交于 2019-12-06 05:31:30
问题 I have a Model with a property [ReadOnly(true)] public decimal BodyMassIndex { get; private set; } In my View when I call @Html.EditorForModel() I still get a standard editable textbox for that property Why is this? if the textbox is still editable whats the point of this DataAnnotation Attibute? Brad Wilson's post 回答1: That is not a DataAnnotation attribute. Notice it's in the System.ComponentModel namespace. Data Annotations are in the System.ComponentModel.DataAnnotations namespace.

Validator.TryValidateObject does not call Remote validation

我们两清 提交于 2019-12-06 05:31:01
I have a class with a remote validation data annotation as follows: public partial class LuInspectionWindow { [Required] public int InspectionWindowId { get; set; } [Required] public string Description { get; set; } [Required] [DataType(DataType.Date)] public DateTime StartDate { get; set; } [Required] [Remote("ValidateWindowEndDate", "InspectionWindow", AdditionalFields = "StartDate")] public DateTime EndDate { get; set; } } That calls this annotation: [AcceptVerbs("Get", "Post")] public IActionResult ValidateWindowEndDate(DateTime? endDate, DateTime? startDate) { int minWeeks = 8; if

asp.net MVC extending DataAnnotions

南楼画角 提交于 2019-12-06 04:34:43
问题 As well as DisplayName eg. [DisplayName("Address line 1 ")] public string Address1{get; set;} Html.LabelFor(model => model.Address1) I have a requirement to show tooltips eg. [DisplayName("Address line 1 ")] [ToolTip("The first line of your address as it appears on you bank statement")] public string Address1{get; set;} Html.LabelFor(model => model.Address1) Html.ToolTipFor(model => model.Address1) Can I extend the DisplayName DataAnnotation to do this? I can't see how it can be done. Thanks!

Can I add MVC 2 DataAnnotation attributes to existing properties?

旧城冷巷雨未停 提交于 2019-12-06 03:14:48
I'm using a generated class as a model, and I wish to add DataAnnotation attributes to some of its properties. As it's a generated code, I don't want to add the annotations directly. Is there another way to attach them to a property? I'd considered making the model an interface, and using a partial class to get the generated class to subscribe to it. Is there a less elaborate solution, assuming that would even work? Yes there is. You have to create metadata class that will have the same properties that your original model, and connect it to your model with MetadataType attribute: [MetadataType

How to access viewmodel's property value in custom validation attribute to alter messages?

て烟熏妆下的殇ゞ 提交于 2019-12-06 03:10:40
问题 The viewmodel has many string properties like Sample as below. My requirement is to show different validation messages depending on a bool flag in my viewmodel. That flag is IsProposer property as mentioned below: [SampleAttribute(true, "bla prop", "foo add driver")] public string Sample { get; set; } public bool IsProposer { get; set; } I thought to create a validation attribute so that I can just place it on all my string properties (required validation). And then depending on the value of

Client-side validation not firing for CompareAttribute DataAnnotation

此生再无相见时 提交于 2019-12-06 02:36:35
问题 I'm laying out a view that compares two password strings. The two properties in one of my models are pretty straightforward: [Required] [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")] [StringLength(20, MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "New Password")] public string NewPassword { get; set; } [Required] [DataType(DataType.Password)] [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")] [StringLength(20,

asp.net MVC 1.0 and 2.0 currency model binding

荒凉一梦 提交于 2019-12-06 02:32:50
问题 I would like to create model binding functionality so a user can enter ',' '.' etc for currency values which bind to a double value of my ViewModel. I was able to do this in MVC 1.0 by creating a custom model binder, however since upgrading to MVC 2.0 this functionality no longer works. Does anyone have any ideas or better solutions for performing this functionality? A better solution would be to use some data annotation or custom attribute. public class MyViewModel { public double