Validator.TryValidateObject Not Validating RangeAttribute

Ah so it would seem I need to specify validateAllProperties = true Validator.TryValidateObject(question, ctx, results, true); Incidentally what was throwing me off was the fact I had an abstract base class with another property in it and without validateAllProperties the Validator will stop on the first error of ALL superclasses too. So you will get … Read more

Custom model validation of dependent properties using Data Annotations

MVC2 comes with a sample “PropertiesMustMatchAttribute” that shows how to get DataAnnotations to work for you and it should work in both .NET 3.5 and .NET 4.0. That sample code looks like this: [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] public sealed class PropertiesMustMatchAttribute : ValidationAttribute { private const string _defaultErrorMessage = “‘{0}’ and ‘{1}’ … Read more

What is use of UIHint attribute in MVC [duplicate]

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 … Read more

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

There are two ways to do this: Models.Test test = new Models.Test(); string DisplayName = test.GetDisplayName(t => t.Name); string DisplayName = Helpers.GetDisplayName<Models.Test>(t => t.Name); The first one works by virtue of writing a generic extension method to any TModel (which is all types). This means it will be available on any object and not just … Read more

Using DataAnnotations to compare two model properties

Make sure that your project references system.web.mvc v3.xxxxx. Then your code should be something like this: using System.Web.Mvc; . . . . [Required(ErrorMessage = “This field is required.”)] public string NewPassword { get; set; } [Required(ErrorMessage = “This field is required.”)] [Compare(nameof(NewPassword), ErrorMessage = “Passwords don’t match.”)] public string RepeatPassword { get; set; }

How to add “required” attribute to mvc razor viewmodel text input editor

You can use the required html attribute if you want: @Html.TextBoxFor(m => m.ShortName, new { @class = “form-control”, placeholder = “short name”, required=”required”}) or you can use the RequiredAttribute class in .Net. With jQuery the RequiredAttribute can Validate on the front end and server side. If you want to go the MVC route, I’d suggest … Read more

How to handle Booleans/CheckBoxes in ASP.NET MVC 2 with DataAnnotations?

My Solution is as follows (it’s not much different to the answers already submitted, but I believe it’s named better): /// <summary> /// Validation attribute that demands that a boolean value must be true. /// </summary> [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)] public class MustBeTrueAttribute : ValidationAttribute { public override bool IsValid(object value) { … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)