data-annotations

What is use of UIHint attribute in MVC [duplicate]

…衆ロ難τιáo~ 提交于 2019-11-27 21:00:58
This question already has an answer here: UIHint Attribute in MVC 2 answers Can anyone please explain me what is the use of UIHint attribute in MVC . Why do we need this. and when and how to use . Thanks UIHintAttribute Specifies the template or user control that Dynamic Data uses to display a data field. This is the MSDN description of UIHintAttribute. It firstly introduced for Dynamic Data applications and ASP.NET MVC also adapted it. If you annotate a property with UIHint attribute and use EditorFor or DisplayFor inside your views, ASP.NET MVC framework will look for the specified template

Validation does not work when I use Validator.TryValidateObject

痞子三分冷 提交于 2019-11-27 20:50:26
DataAnnotations does not work with buddy class. The following code always validate true. Why ? var isValid = Validator.TryValidateObject(new Customer(), Context, results, true); and here is the buddy class. public partial class Customer { public string Name { get; set; } public int Age { get; set; } } [MetadataType(typeof(CustomerMetaData))] public partial class Customer { public class CustomerMetaData { [Required(ErrorMessage = "You must supply a name for a customer.")] public string Name { get; set; } } } Here is another thread with same question., but no answer. link text I found the answer

Data Annotation Ranges of Dates

做~自己de王妃 提交于 2019-11-27 20:22:07
Is it possible to use [Range] annotation for dates? something like [Range(typeof(DateTime), DateTime.MinValue.ToString(), DateTime.Today.ToString())] Daniel Elliott Docs on MSDN says you can use the RangeAttribute [Range(typeof(DateTime), "1/2/2004", "3/4/2004", ErrorMessage = "Value for {0} must be between {1} and {2}")] public datetime Something { get; set;} I did this to fix your problem public class DateAttribute : RangeAttribute { public DateAttribute() : base(typeof(DateTime), DateTime.Now.AddYears(-20).ToShortDateString(), DateTime.Now.AddYears(2).ToShortDateString()) { } } jQuery

Create database index with Entity Framework

与世无争的帅哥 提交于 2019-11-27 19:54:42
Say I have the following model: [Table("Record")] public class RecordModel { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] [Display(Name = "Record Id")] public int RecordId { get; set; } [StringLength(150)] public string Name { get; set; } [Required] [StringLength(15)] public string IMEI { get; set; } } Is it possible to add an index to the IMEI column through using an attribute, data annotation, or something from the model? MikroDel According to this link: Creating Indexes via Data Annotations with Entity Framework 5.0 you should write some kind of extension code: using

MVC unobtrusive range validation of dynamic values

↘锁芯ラ 提交于 2019-11-27 19:28:53
I have a value on my model, that must fall within the range of two other values on my model. For example: public class RangeValidationSampleModel { int Value { get; set; } int MinValue { get; set; } int MaxValue { get; set; } } Of course, I can't pass these Min/MaxValues into my DataAnnotations attributes, as they have to be constant values. I'm sure I need to build my own validation attribute, but I haven't done this much and can't wrap my mind around how it should work. I've searched for about an hour, and have seen all sorts of solutions for building custom validation, but can't find

Providing localized error messages for non-attributed model validation in ASP.Net MVC 2?

本秂侑毒 提交于 2019-11-27 19:07:12
I'm using the DataAnnotations attributes along with ASP.Net MVC 2 to provide model validation for my ViewModels: public class ExamplePersonViewModel { [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resources.Validation))] [StringLength(128, ErrorMessageResourceName = "StringLength", ErrorMessageResourceType = typeof(Resources.Validation))] [DataType(DataType.Text)] public string Name { get; set; } [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resources.Validation))] [DataType(DataType.Text)] public int Age { get; set; } }

DataAnnotation for Required property

淺唱寂寞╮ 提交于 2019-11-27 18:44:41
First it works, but today it failed! This is how I define the date property: [Display(Name = "Date")] [Required(ErrorMessage = "Date of Submission is required.")] [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)] [DataType(DataType.Date)] public DateTime TripDate { get; set; } It has been working in the past. But today, when I call the same ApiController action: [HttpPost] public HttpResponseMessage SaveNewReport(TripLeaderReportInputModel model) The Firebug reports: ExceptionMessage: "Property 'TripDate' on type 'Whitewater.ViewModels.Report.TripLeaderReportInputModel'

ASP.Net MVC DisplayFormat

二次信任 提交于 2019-11-27 18:41:54
In my model I have the following DataAnnotations on one of my properties [Required(ErrorMessage = "*")] [DisplayFormat(DataFormatString = "{0:d}")] [DataType(DataType.Date)] public DateTime Birthdate { get; set; } The required annotation works great, I added the other 2 to try and remove the time. It gets bound to an input in the view using <%=Html.TextBoxFor(m => m.Birthdate, new { @class = "middle-input" })%> However whenever the view loads I still get the time appearing in the input box. Is there anyway to remove this using DataAnnotations? The [DisplayFormat] attribute is only used in

mvc4 data annotation compare two dates

家住魔仙堡 提交于 2019-11-27 18:22:09
I have these two fields in my model: [Required(ErrorMessage="The start date is required")] [Display(Name="Start Date")] [DisplayFormat(DataFormatString = "{0,d}")] public DateTime startDate { get; set; } [Required(ErrorMessage="The end date is required")] [Display(Name="End Date")] [DisplayFormat(DataFormatString = "{0,d}")] public DateTime endDate{ get; set; } I require that endDate must be greater than startDate . I tried using [Compare("startDate")] but this only works for the equal operation. What should I use for the "greater than" operation? Take a look at Fluent Validation or MVC

Get [DisplayName] attribute of a property in strongly-typed way

白昼怎懂夜的黑 提交于 2019-11-27 17:20:38
Good day! I've such method to get [DisplayName] attribute value of a property (which is attached directly or using [MetadataType] attribute). I use it in rare cases where I need to get [DisplayName] in controller code. 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