data-annotations

Range annotation between nothing and 100?

怎甘沉沦 提交于 2019-12-01 14:40:02
问题 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

MVC Foolproof Validation using PassOnNull - Nullable or “01.01.0001 00:00:00”

不问归期 提交于 2019-12-01 14:09:07
I try tovalidate two dates (start -> end) where only the first ist required, but when the user enters the second date it must be greater than the first. I'm using the MVC Foolproof package with the "PassOnNull" parameter. Model <Required()> _ <DisplayName("Event Start")> _ <DataType(DataType.DateTime)> _ 'This doesn't work: Public Property EventStart As Nullable(Of DateTime) 'This does work but with the ugly default value in the textbox Public Property EventStart As DateTime <DisplayName("Event End")> _ <DataType(DataType.DateTime)> _ <GreaterThan("EventStart", PassOnNull:=True)> _ Public

MVC Foolproof Validation using PassOnNull - Nullable or “01.01.0001 00:00:00”

三世轮回 提交于 2019-12-01 13:10:53
问题 I try tovalidate two dates (start -> end) where only the first ist required, but when the user enters the second date it must be greater than the first. I'm using the MVC Foolproof package with the "PassOnNull" parameter. Model <Required()> _ <DisplayName("Event Start")> _ <DataType(DataType.DateTime)> _ 'This doesn't work: Public Property EventStart As Nullable(Of DateTime) 'This does work but with the ugly default value in the textbox Public Property EventStart As DateTime <DisplayName(

MVC 3 client-side validation on collection with data annotations — not working

百般思念 提交于 2019-12-01 13:03:58
I am trying to get client side validation to work on my model -- so far it's not working. My model has a property that's a collection: public class NewsEventsModel { public List<NewsItemDetails> newsItems { get; set; } public int pageNumber { get; set; } public int totalPages { get; set; } public bool canManageNews { get; set; } public long userID { get; set; } } and NewsItemDetails is defined thusly: public class NewsItemDetails { public long itemID { get; set; } public long postedByID { get; set; } public DateTime datePosted { get; set; } [Required(ErrorMessage = "Please enter news or event

Adding DataAnnotation to class when using FluentValidation

微笑、不失礼 提交于 2019-12-01 11:17:45
I use the FluentValidation framework to add validation and annotations to my models in an MVC project. I need to add data annotations to the class level of a model. Namely, the model needs to have the DisplayColumn attribute added. But, since I use FluentValidation (and have the application's ModelMetadataProvider set to use FluentValidation), even if I put the DisplayColumn attribute on the model class, it isn't used. However, I can't find a way to add that annotation by using FluentValidation. Does anyone have any idea how I can get that to work? Thanks I wouldn't actually recommend using

Data annotations, why does boolean prop.IsRequired always equal true

余生颓废 提交于 2019-12-01 11:13:41
I have a model containing a boolean with no [Required] attribute public bool IsOptedIn { get; set; } I have overriden Object.cshtml as follows and am using @Html.EditorForModel() to generate my form @{ var properties = ViewData.ModelMetadata.Properties .Where(prop => prop.ShowForEdit && !ViewData.TemplateInfo.Visited(prop)); } @foreach (var prop in properties) { var hasModelStateError = ViewContext.ViewData.ModelState.Any(m => m.Key == prop.PropertyName) && ViewContext.ViewData.ModelState[prop.PropertyName].Errors != null && ViewContext.ViewData.ModelState[prop.PropertyName].Errors.Count > 0;

Data annotations, why does boolean prop.IsRequired always equal true

ぐ巨炮叔叔 提交于 2019-12-01 08:20:59
问题 I have a model containing a boolean with no [Required] attribute public bool IsOptedIn { get; set; } I have overriden Object.cshtml as follows and am using @Html.EditorForModel() to generate my form @{ var properties = ViewData.ModelMetadata.Properties .Where(prop => prop.ShowForEdit && !ViewData.TemplateInfo.Visited(prop)); } @foreach (var prop in properties) { var hasModelStateError = ViewContext.ViewData.ModelState.Any(m => m.Key == prop.PropertyName) && ViewContext.ViewData.ModelState

Adding DataAnnotation to class when using FluentValidation

时间秒杀一切 提交于 2019-12-01 08:03:06
问题 I use the FluentValidation framework to add validation and annotations to my models in an MVC project. I need to add data annotations to the class level of a model. Namely, the model needs to have the DisplayColumn attribute added. But, since I use FluentValidation (and have the application's ModelMetadataProvider set to use FluentValidation), even if I put the DisplayColumn attribute on the model class, it isn't used. However, I can't find a way to add that annotation by using

ASP.NET MVC custom multiple fields validation

夙愿已清 提交于 2019-12-01 07:54:21
I'm developing an ASP.NET MVC 5.2.3 custom data annotation for validation in Visual Studio 2015. It needs to take any number of fields and ensure that if one has a value, they all must have a value; if they're all null/blank, it should be okay. A few examples have helped: ASP.NET MVC implement custom validator use IClientValidatable MVC Form Validation on Multiple Fields http://www.macaalay.com/2014/02/24/unobtrusive-client-and-server-side-age-validation-in-mvc-using-custom-data-annotations/ However, I'm not sure how to do the client-side validation where you have an unknown number of fields

Custom DataAnnotations Validator Derived from RegularExpressionAttribute

风格不统一 提交于 2019-12-01 06:07:54
The Gu provides an example of how you might create a custom validator that overrides RegularExpressionAttribute . The advantage of this is that you don't have to create a custom Model Validator but I can't get it to work. Given the following code: public class NameAttribute : RegularExpressionAttribute { public NameAttribute() : base(@"^[\w\s\-\']+$") { } } This works: [RegularExpression(@"^[\w\s\-\']+$")] But this doesn't: [Name] Have I misunderstood an aspect of Scott's example or is the example flawed in that MVC doesn't support derived types out of the box, so actually I will have to