data-annotations

Data annotations MVC3 Required attribute

余生长醉 提交于 2019-12-01 20:06:48
I have the Model (User) below, I use it to add new users and to update existing users. When I'm adding a new user it's required to enter the user name and the password, and when I'm updating it's required to enter only the user name because it's not allowed to change the password. Here is the problem, adding a new user everything works ok because I enter both name and password values so ModelState.IsValid returns true, but when updating an user there is no input to the password, so it always have the null value what makes the ModelState.IsValid returns false all the time. Is there a way to use

Data Annotation/Validation and dynamic values

喜欢而已 提交于 2019-12-01 19:27:35
If some of my models have dynamic validation conditions (i.e. the string length can be minimum of 8 or 12 depending on a database value or some other dynamic value) is it impossible to use data annotation for validation? From what I understand, the values of any parameter (example StringLength min/max value) have to be truly static. Are there alternatives for applications that have dynamic validation values? Your pretty much stuck with writing your own custom validationattribute: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.aspx 来源: https:/

How to replace standard DataAnnotations error messages

妖精的绣舞 提交于 2019-12-01 17:40:28
I'm using System.ComponontModel.DataAnnotations to validate my model objects. How could I replace messages standard attributes (Required and StringLength) produce without providing ErrorMessage attribute to each of them or sub classing them? Vitaliy Ulantikov Writing new post because I need more formatting than comments provide. Look at ValidationAttribute - base class of validation attributes. If validation error occured, error message will be created by method: public virtual string FormatErrorMessage(string name) { return string.Format(CultureInfo.CurrentCulture, this.ErrorMessageString,

Custom validation attribute with multiple instances problem

元气小坏坏 提交于 2019-12-01 17:31:23
问题 I'm using tha namespace System.ComponentModel.DataAnnotations in C# 4 to implement my own validation attribute and it looks like this [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public sealed class MyCustomValidator : ValidationAttribute { private String Property1 { get; set; } private String Property2 { get; set; } public ValeTaxiSituacaoRequired(String property1, String property2) { Property1 = property1; Property2 = property2; } public override bool IsValid(object value)

ASP.net MVC Data Annotations DateTime Default Value

折月煮酒 提交于 2019-12-01 17:04:26
In my view model i have the following attribute: [Required] [DataType(DataType.Date, ErrorMessage="Please enter a valid date in the format dd/mm/yyyy")] [Display(Name = "Date of Birth")] public DateTime DOB { get; set; } In my view i have the following: <div class="editor-label"> @Html.LabelFor(model => model.DOB) </div> <div class="editor-field"> @Html.EditorFor(model => model.DOB) @Html.ValidationMessageFor(model => model.DOB) </div> Before submitting the form the default value for DOB is 1/01/0001 how do i stop this value being auto populated, i simply want an empty field when people visit

DisplayAttribute name with a variable, Dynamic DisplayName

我是研究僧i 提交于 2019-12-01 16:40:00
问题 Wondering if this is possible or something with this effect. public class MyModel { public string Name { get; set; } [Display(Name = String.Format("This is [0]'s phone number", Name)] public string PhoneNumber { get; set; } } I'm talking about a DisplayName with a variable in it, non static and possibly based on the models other properties. Is this possible in any way? 回答1: This isn't possible because the arguments specified for parameters of attributes must be constant values (instinctively,

ASP.net MVC Data Annotations DateTime Default Value

℡╲_俬逩灬. 提交于 2019-12-01 16:25:35
问题 In my view model i have the following attribute: [Required] [DataType(DataType.Date, ErrorMessage="Please enter a valid date in the format dd/mm/yyyy")] [Display(Name = "Date of Birth")] public DateTime DOB { get; set; } In my view i have the following: <div class="editor-label"> @Html.LabelFor(model => model.DOB) </div> <div class="editor-field"> @Html.EditorFor(model => model.DOB) @Html.ValidationMessageFor(model => model.DOB) </div> Before submitting the form the default value for DOB is 1

how to validate input file with jquery and dataannotation in asp.net mvc 3

倾然丶 夕夏残阳落幕 提交于 2019-12-01 16:04:08
I am sure I am missing something here, I found this question to validate a file, here is the sample code public class UpdateSomethingViewModel { [DisplayName("evidence")] [Required(ErrorMessage="You must provide evidence")] [RegularExpression(@"^abc123.jpg$", ErrorMessage="Stuff and nonsense")] public HttpPostedFileBase Evidence { get; set; } } but I don't see any @Html.FileFor(model => model.Evidence) Any ideas? Update I found a simple solution passing attribute type in html attribute collection. @Html.TextBoxFor(model => model.Evidence, new { type = "file" }) @Html.ValidationMessageFor(model

how to validate input file with jquery and dataannotation in asp.net mvc 3

孤街浪徒 提交于 2019-12-01 15:15:15
问题 I am sure I am missing something here, I found this question to validate a file, here is the sample code public class UpdateSomethingViewModel { [DisplayName("evidence")] [Required(ErrorMessage="You must provide evidence")] [RegularExpression(@"^abc123.jpg$", ErrorMessage="Stuff and nonsense")] public HttpPostedFileBase Evidence { get; set; } } but I don't see any @Html.FileFor(model => model.Evidence) Any ideas? Update I found a simple solution passing attribute type in html attribute

Range annotation between nothing and 100?

别等时光非礼了梦想. 提交于 2019-12-01 15:04:01
I have a [Range] annotation that looks like this: [Range(0, 100)] public int AvailabilityGoal { get; set; } My webpage looks like this: <%=Html.TextBoxFor(u => u.Group.AvailabilityGoal)%> It works as it should, I can only enter values between 0 and 100 but I also want the input box to be optional, the user shouldn't get an validation error if the input box is empty. This has nothing to do with the range but because the type is an integer. If the user leaves it empty it should make AvailabilityGoal = 0 but I don't want to force the user to enter a zero. I tried this but it (obviously) didn't