data-annotations

how to put DisplayName on ErrorMessage format

我怕爱的太早我们不能终老 提交于 2019-11-27 12:10:02
问题 I have something like this: [DisplayName("First Name")] [Required(ErrorMessage="{0} is required.")] [StringLength(50, MinimumLength = 10, ErrorMessage="{0}'s length should be between {2} and {1}.")] public string Name { get; set; } I want to have the following output: First Name is required. First Name's length should be between 10 and 50. It is working when using ASP.NET MVC2 Error Summary , but when I try to validate it manually, like this: ValidationContext context = new ValidationContext

Entity Framework Code First : how to annotate a foreign key for a “Default” value?

痞子三分冷 提交于 2019-11-27 12:04:12
I have 2 classes: Client and Survey. Each Client can have many surveys - but only one default survey. I have defined the classes like this: public class Client { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ID { get; set; } public string ClientName { get; set; } public Nullable<int> DefaultSurveyID { get; set; } [ForeignKey("DefaultSurveyID")] public virtual Survey DefaultSurvey { get; set; } public virtual ICollection<Survey> Surveys { get; set; } } public class Survey { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ID { get; set; } public string

MVC: Override default ValidationMessage

耗尽温柔 提交于 2019-11-27 11:18:24
问题 In the world of MVC I have this view model... public class MyViewModel{ [Required] public string FirstName{ get; set; } } ...and this sort of thing in my view... <%= Html.ValidationSummary("Please correct the errors and try again.") %> <%= Html.TextBox("FirstName") %> <%= Html.ValidationMessage("FirstName", "*") %> My question: If I submit this form without supplying a name, I get the following message "The FirstName field is required" OK. So, I go and change my property to... [DisplayName(

MVC 5 Remote Validation

对着背影说爱祢 提交于 2019-11-27 11:14:00
问题 I need to validate an input field value from user before the form is submitted. I have created an action in my custom controller and decorated the field with it: action name: CheckValue controller name: Validate [Remote("CheckValue", "Validate"), ErrorMessage="Value is not valid"] public string Value { get; set; } The problem is when I press submit, the form is being submitted and then the message Value is not valid is shown if the value entered by the user is not valid. How can I validate

How can I use the Data Validation Attributes in C# in a non-ASP.net context?

不羁岁月 提交于 2019-11-27 11:02:59
I'd like to use the data validation attributes in a library assembly, so that any consumer of the data can validate it without using a ModelBinder (in a console application, for instance). How can I do it? TheCodeKing Actually this is pretty cool. I used it in a WFP validation implementation recently. Most people end up writing lots of code using reflection to iterate the attributes, but there's a built in function for this. var vc = new ValidationContext(myObject, null, null); return Validator.TryValidateObject(myObject, vc, null, true); You can also validate attributes on a single named

Best Practices ViewModel Validation in ASP.NET MVC

谁都会走 提交于 2019-11-27 10:52:48
I am using DataAnnotations to validate my ViewModel on client side with jquery.validate.unobtrusive and on server side in ASP.NET MVC application. Not so long time ago, I figured out that I can write validation like this: [Required(ErrorMessage = "{0} is required")] public string Name { get; set; } That way I can easily define some general strings in config or in resources and always use it in DataAnnotations . So it will be easier to change validation messages in my whole application in future. Also I know that there is a FluentValidation library that allows to add validation rules to already

Unit Testing ASP.NET DataAnnotations validation

人盡茶涼 提交于 2019-11-27 10:43:33
I am using DataAnnotations for my model validation i.e. [Required(ErrorMessage="Please enter a name")] public string Name { get; set; } In my controller I am checking the value of ModelState. This is correctly returning false for invalid model data posted from my view. However, when executing the unit test of my controller action, ModelState always returns true: [TestMethod] public void Submitting_Empty_Shipping_Details_Displays_Default_View_With_Error() { // Arrange CartController controller = new CartController(null, null); Cart cart = new Cart(); cart.AddItem(new Product(), 1); // Act var

MetadataType problem

空扰寡人 提交于 2019-11-27 09:21:26
I'm using VS2008 SP1, WCF Ria Service July 2009 CTP. I found out that MetadataType does not work in partial class mode, really don't know what I have missed out: Work:- public partial class Person { private string _Name; [Required(AllowEmptyStrings=false, ErrorMessage="Name required entry")] [StringLength(3)] public string Name { set{_Name = value;} get{return _Name;} } } class Program { static void Main(string[] args) { Person p = new Person { Name="123432" }; List res = new List(); Validator.TryValidateObject(p,new ValidationContext(p,null,null), res,true); if (res.Count > 0) { Console

How do I specify that a property should generate a TEXT column rather than an nvarchar(4000)

走远了吗. 提交于 2019-11-27 07:56:52
I'm working with the Code First feature of Entity Framework and I'm trying to figure out how I can specify the column data types that should be created when the database is auto-generated. I have a simple model: public class Article { public int ArticleID { get; set; } public string Title { get; set; } public string Author { get; set; } public string Summary { get; set; } public string SummaryHtml { get; set; } public string Body { get; set; } public string BodyHtml { get; set; } public string Slug { get; set; } public DateTime Published { get; set; } public DateTime Updated { get; set; }

Unable to set membernames from custom validation attribute in MVC2

独自空忆成欢 提交于 2019-11-27 07:41:30
问题 I have created a custom validation attribute by subclassing ValidationAttribute. The attribute is applied to my viewmodel at the class level as it needs to validate more than one property. I am overriding protected override ValidationResult IsValid(object value, ValidationContext validationContext) and returning: new ValidationResult("Always Fail", new List<string> { "DateOfBirth" }); in all cases where DateOfBirth is one of the properties on my view model. When I run my application, I can