data-annotations

How to replace standard DataAnnotations error messages

半城伤御伤魂 提交于 2019-12-04 03:20:44
问题 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? 回答1: 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

So is an [Email] attribute built in ASP.NET MVC 3 or not?

橙三吉。 提交于 2019-12-04 02:32:10
The [Email] attribute was going to be built into ASP.NET MVC 3 as it was in futures? So is it now available or not? I guess it is quite a dumb question but I've spent some time googling and didn't find any normal answer. The email regex: "^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-

How Can I Use Custom Validation Attributes on Child Models of a DB Entity?

半城伤御伤魂 提交于 2019-12-04 00:28:52
问题 Summary: I want a data annotation validator to reference another property in the same class ( TitleAuthorAndPublishingConfiguration ). However, DB.SaveChanges() is not being called on this class directly. Rather it is being called on the parent of this class ( WebsiteConfiguration ). Therefore validationContext.ObjectType is returning WebsiteConfiguration and I am unable to refer to properties of TitleAuthorAndPublishingConfiguration within the data annotation validator. WebsiteConfiguration

Asp.Net MVC EnableClientValidation doesn't work

£可爱£侵袭症+ 提交于 2019-12-03 23:51:19
问题 I want as well as Client Side Validation as Server Side Validation. I realized this as the following: Model: ( The model has a DataModel(dbml) which contains the Test class ) namespace MyProject.TestProject { [MetadataType(typeof(TestMetaData))] public partial class Test { } public class TestMetaData { [Required(ErrorMessage="Please enter a name.")] [StringLength(50)] public string Name { get; set; } } } Controller is nothing special. The View: <% Html.EnableClientValidation(); %> <% using

MV3 input validation - IE8 & IE9 behave differently

最后都变了- 提交于 2019-12-03 21:40:31
I'm using DataAnnotations to validate my input fields on a MVC3 application. I'm using regular expressions validations. I get the validation messages on the UI for IE8 & IE9. But I notice the difference when I hit the Save button even after the client side validation has failed. IE9 keeps me on the client side. On IE8 however, the control goes to the controller action, and I have to have a controller side TryValidateModel so that the validation errors out. Does anyone know why IE8 is doing a server round trip? Edit: Adding the code. This goes into the cshtml. @using (Html.BeginForm("Person",

Is it possible to reuse the DataAnnotations in ViewModel?

懵懂的女人 提交于 2019-12-03 20:35:14
In my MVC application, I defined the DataAnnotations in the domain models. Although the DataAnnotations properties as Display, etc. can be retrieved when using Domain model, they cannot be retrieved when using the same properties on ViewModel and using this ViewModel. I think it is not seem to good to define the DataAnnotations in ViewModel again. So, is it possible or which way should I follow? Domain Model: public class Issue { [Key] public int ID { get; set; } [Required(ErrorMessage = "Required")] [Display(Name = "Project Number")] public int ProjectID { get; set; } [Required(ErrorMessage =

How can I validate Mobile/Landline Number formatting using MVC.Net?

人盡茶涼 提交于 2019-12-03 20:22:25
Pretty simple question - validating International Mobile/Landline Number using Data Annotations. Any suggestions? There is no single "international" phone number standard, but you can do some validation. For example: [RegularExpression(@"^([\+]|0)[(\s]{0,1}[2-9][0-9]{0,2}[\s-)]{0,2}[0-9][0-9][0-9\s-]*[0-9]$")] This allows, for example: 044 123-456 , +2 12-12456 , +(234) 56-56-452 . See here for more kinds of regular expressions for phone numbers: http://regexlib.com/Search.aspx?k=phone+number&c=0&m=0&ps=20&p=12 MVC has built in support for telephone numbers: [DataType(DataType.PhoneNumber)]

ASP.NET MVC, Linq to SQL Data Annotation Validation

扶醉桌前 提交于 2019-12-03 18:30:33
问题 I'm trying implement Data Annotation to my Linq to SQL objects. The .dbml file is generated and I'm not sure how to add data annotation to the objects without touching the generated source code. I tried to add data annotations to the a separate partial class of the object, but its not recognizing it, no Intelli sense either. 回答1: As I said in my original answer to this question, you should use an interface. The answer posted after mine (which was marked as Accepted) said to use a class. This

MVC3 DataAnnotationsExtensions error using numeric attribute

蓝咒 提交于 2019-12-03 17:24:13
I've installed Scott's Kirkland DataAnnotationsExtensions. In my model I have: [Numeric] public double expectedcost { get; set; } And in my View: @Html.EditorFor(model => model.expectedcost) Now, when the page tries to render I get the following error: Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: number Any ideas why I'm getting the error ? The quick answer is simply remove the attribute [Numeric] The longer explanation is that by design, validation already adds a data-val-number because it's of type double

Enable/Disable mvc server side validation dynamically

痞子三分冷 提交于 2019-12-03 16:58:37
I have an mvc form with multiple submit buttons - 'Save Draft' and 'Publish'. The objective is to skip both client side(javascript/unobstructive) validation and server side validation when the 'Save Draft' button is clicked and the form is submitted. But I do need to trigger both validation if the 'Publish' button is clicked. My research has led me to few solutions. Client Side -By writing a jquery plugin (function ($) { $.fn.turnOffValidation = function (form) { var settings = form.validate().settings; for (var ruleIndex in settings.rules) { delete settings.rules[ruleIndex]; } }; })(jQuery);