asp.net-mvc-validation

MVC3 How to define custom error messages using annotations?

半腔热情 提交于 2019-12-11 08:54:57
问题 I have a DateTime field(that can accept multiple date time formats, so it is pain to create Regex patter) When in the field I'm entering something like "Aaaaaa", I'm getting error message: The value 'Aaaaa' is not valid for OwnerBirthDate Model looks: [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}")] [Required(ErrorMessage = "*")] public DateTime? OwnerBirthDate { get; set; } View: @Html.TextBoxFor(x => x.OwnerBirthDate) How can I define custom error message

Is DataTypeAttribute is a validation attribute For DefaultModelBinder Class

自闭症网瘾萝莉.ら 提交于 2019-12-11 08:44:29
问题 I have just noticed that DataTypeAttribute class is inherited from System.ComponentModel.DataAnnotations.ValidationAttribute . In terms of ASP.NET MVC DefaultModelBinder class, is DataTypeAttribute is a validation attribute? In plain English, does ModelBinder validate the object according to DataTypeAttribute ? For example, if I specify DataType property to DataType.EmailAddress , will it validate the e-mail address or this attribute is only providing metadata for objects. UPDATE I found a

Returning a list of keys with ModelState errors

送分小仙女□ 提交于 2019-12-10 14:32:31
问题 How can I return a list/array of all keys that have an error? I have tried to do the below, but it says I can't have that sort of expression for some reason. ModelState.ToList(item => item.Value.Errors.Count > 0) 回答1: var errors = from modelstate in ModelState.AsQueryable().Where(f => f.Value.Errors.Count > 0) select new { Title = modelstate.Key }; 回答2: Count is a method. You need ()s after is. But I'd prefer Any, anyway: from item in ModelState where item.Value.Errors.Any() select item.Key

DRY Remote Validation in ASP.NET MVC 3

霸气de小男生 提交于 2019-12-09 07:39:27
I've read David Hayden's great post on MVC 3 Remote validation . However there is presented what you should do to enable remote (javascript) validation. If the user has javascript disabled the post would still be made even if data is not valid. Therefore a server-side validation should occur. How could we make this check as DRY (Don't Repeat Yourself) as possible? Of course, including the same check code in the post action as in the remote validation action (or just the same call) can work but I am wondering if a one-liner or something more elegant is available. Perfectly acceptable answers

DRY Remote Validation in ASP.NET MVC 3

感情迁移 提交于 2019-12-08 07:14:48
问题 I've read David Hayden's great post on MVC 3 Remote validation. However there is presented what you should do to enable remote (javascript) validation. If the user has javascript disabled the post would still be made even if data is not valid. Therefore a server-side validation should occur. How could we make this check as DRY (Don't Repeat Yourself) as possible? Of course, including the same check code in the post action as in the remote validation action (or just the same call) can work but

Client side validation not working for hidden field in asp.net mvc 3

前提是你 提交于 2019-12-06 21:49:28
问题 I have got a hidden field with a validation for it as below @Html.HiddenFor(m => m.Rating) @Html.ValidationMessageFor(m => m.Rating) The Rating property has Range validator attribute applied with range being 1-5. This is put inside a form with a submit button. I have then got following jquery that sets the value in hidden field on some user event (Basically user clicks on some stars to rate) $(".star").click(function(){ $("#Rating").val(2); }); Now if I submit the form without the user event

trying to inherit RegularExpressionAttribute, no longer validates

人盡茶涼 提交于 2019-12-06 21:21:38
问题 I'm trying to inherit the RegularExpressionAttribute to improve reusability with validating SSNs. I have the following model: public class FooModel { [RegularExpression(@"^(?!000)(?!666)(?!9[0-9][0-9])\d{3}[- ]?(?!00)\d{2}[- ]?(?!0000)\d{4}$", ErrorMessage = "The SSN you entered is invalid. If you do not have this number please leave the field blank")] public string Ssn { get; set; } } which will validate correctly on the client and server. I wanted to encapsulate that lengthy regular

Client side validation not working for hidden field in asp.net mvc 3

三世轮回 提交于 2019-12-05 02:42:02
I have got a hidden field with a validation for it as below @Html.HiddenFor(m => m.Rating) @Html.ValidationMessageFor(m => m.Rating) The Rating property has Range validator attribute applied with range being 1-5. This is put inside a form with a submit button. I have then got following jquery that sets the value in hidden field on some user event (Basically user clicks on some stars to rate) $(".star").click(function(){ $("#Rating").val(2); }); Now if I submit the form without the user event that sets the hidden field, the validation works. The error messages is displayed properly and it works

ASP.NET MVC TryValidateModel() Issues when Model is Modified

╄→尐↘猪︶ㄣ 提交于 2019-12-04 16:22:15
问题 I have a two step form process where the first set of data is stored in session. [IsMp4File] [Required(ErrorMessage = "* Please select a video to upload")] public HttpPostedFileBase VideoClip { get; set; } [Required(ErrorMessage = "* Please select a thumbmail image")] public HttpPostedFileBase VideoThumbnail{ get; set; } public string VideoFileName { get { return VideoClip.FileName; } } public NewsWizardStep CurrentStep { get; set; } ... public enum NewsWizardStep : int { One = 1, Two = 2,

Dynamic validation on MVC 2

好久不见. 提交于 2019-12-04 11:59:28
This works fine [MetadataType(typeof(Area_Validation))] public partial class Area { ... } public class Area_Validation { [Required(ErrorMessage = "Please add this field.")] public int Email { get; set; } [Required(ErrorMessage = "Please add this field")] public string Name { get; set; } } but how about if Area_Validation is dynamically created ? for example Subscription Fields that on back-end can be created by the user and end up like this: How can I set the Metadata on each field for auto validation ? Currently I'm doing: public class SubscriberFormViewModel { public List