data-annotations

Getting date time always null back to controller in MVC3

岁酱吖の 提交于 2019-12-11 17:27:34
问题 Using MVC3 and jQuery plugin datepicker. I can see calender and I am able to set the date to the field. But when I post model back to controller, I am always getting null value for the property. And it is displaying that particular date as a not valid date. This is my model. [DataType(DataType.DateTime)] [DisplayName("PostTime")] [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] public DateTime? PostTime { get; set; } And part of my view @using (Html.BeginForm

Using Data Annotations to make a field required while searching for another in a form mvc 3

元气小坏坏 提交于 2019-12-11 17:26:57
问题 I have a form with multiple search filters. Let's say Order Id, PO Num, Store Code, Status, etc. I would like to make Store Code required while user searches using PO Num. Is it possible using Data Annotation in asp.net mvc 3? 回答1: One option is you have your own custom validation and implement IValidatableObject See: ASP.NET MVC Conditional validation Note though this is only on the server side. There will be no client side validation here. If you want client side validation as well on this

How to create an custom remote validation attribute with dynamic additiional fields in ASP.NET MVC

本小妞迷上赌 提交于 2019-12-11 13:46:39
问题 I am developing an ASP.NET MVC5 project. In my project I am creating remote validation. For that I just created an custom validation attribute to support server-side validation. My remote validator working fine when remote action has to catch only one field. But I am having problems when I try to bind additional field for it. I mean when I validate with extra fields. Here is my custom remote validation attribute public class RemoteClientServerAttribute : RemoteAttribute { protected override

How do I set the “Display(Name = ”“)” data annotation on dropdownlists from the source model?

寵の児 提交于 2019-12-11 13:30:31
问题 When I try to set the Display(Name = "") attribute on a foreign key field in the model, the Display Name does not render in the view (i.e. the Display Name remains as the column name in the model). See the example below: [Display(Name = "Phone Number")] public long Phone_Number { get; set; } [Display(Name = "Phone Type")] public int Phone_Type_Name { get; set; } Note: The Phone_Type_Name field is a foreign key of the model Phone_Type . The view for this model does not render Phone Type , but

Reusing a column for a required property with Entity Framework 6.0, Fluent API, and DataAnnotations

≡放荡痞女 提交于 2019-12-11 12:19:08
问题 I have a base class public class BaseClass { public int Id {get; set;} } and two derived classes public class Foobar: BaseClass { [Required] public int Whatever {get; set;} } public class Snafu: BaseClass { [Required] public int Whatever {get; set;} } I'm using Table Per Hierarchy inheritance and trying to cut down on my duplicate columns, so with Fluent API I've mapped them like so: modelBuilder.Entity<Foobar>().Property(fb => fb.Whatever).HasColumnName("Whatever"); modelBuilder.Entity<Snafu

Defining the CultureInfo of a property using DataAnnotation

流过昼夜 提交于 2019-12-11 10:57:45
问题 I'm working on a MVC4 application where there's a field with a Data Mask like 000000,00 . Since the main culture of the project is pt-BR, this is correct and works as expected. The problem is that some of our developers have their cultures set to english, and when submitting this to a server set as en-US, a validation error occurs, because the method expects 000000.00 instead of 000000,00 . I thought about making a js fix to check the browser language and change the mask accordingly, but if

Make MVC class property/class required conditionally

吃可爱长大的小学妹 提交于 2019-12-11 10:56:45
问题 I have an Address class that is used for both a MailingAddress and BillingAddress property in my Model. I want the MailingAddress to be required, but not the BillingAddress, but am not seeing a way to do this with DataAnnotations. If I were able to set the [Required] attribute on the MailingAddress property and somehow define the logic for how the Address class is supposed to handle the required logic, I feel like that would be a simple solution. Any ideas? 回答1: If your question is how to use

ASP.NET MVC - “Validation type names must be unique.”

萝らか妹 提交于 2019-12-11 10:15:49
问题 I use ASP.NET MVC 3 and Data Annotations in my model, and want to retrieve the Error Messages from a database. So I wrote inherited attributes: public class LocalizedRequiredAttribute : RequiredAttribute { public LocalizedRequiredAttribute(){} public override string FormatErrorMessage(string name) { return GetByKeyHelper.GetByKey(this.ErrorMessage); } } public class LocalizedRegularExpressionAttribute : RegularExpressionAttribute { public LocalizedRegularExpressionAttribute(string pattern) :

Range data annotation is not working as expected, any idea?

给你一囗甜甜゛ 提交于 2019-12-11 09:06:27
问题 I assume the validation check will allow any input value from 1 to 30. But, it prompts for error when I input a value with 4 to 9 in this field. Any advise? Please... remarks: My project is based on "MVC4 Web Application + Razor View Engine + C# .net". using System.ComponentModel.DataAnnotations; [Range(1, 30, ErrorMessageResourceType = typeof(Resources.ErrorMessage), ErrorMessageResourceName = "RangeErrorMsg")] public int HowManyDays { get; set; } Below is my Razor view source <div class=

Is DataTypeAttribute is a validation attribute For DefaultModelBinder Class

自闭症网瘾萝莉.ら 提交于 2019-12-11 08:44:29
问题 I have just noticed that DataTypeAttribute class is inherited from System.ComponentModel.DataAnnotations.ValidationAttribute . In terms of ASP.NET MVC DefaultModelBinder class, is DataTypeAttribute is a validation attribute? In plain English, does ModelBinder validate the object according to DataTypeAttribute ? For example, if I specify DataType property to DataType.EmailAddress , will it validate the e-mail address or this attribute is only providing metadata for objects. UPDATE I found a