data-annotations

Custom Validation Attribute MVC2

狂风中的少年 提交于 2019-11-30 13:40:20
I have a custom validation attribute which checks to see if two properties have the same values or not (like password and retype password): [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] public class EqualToPropertyAttribute : ValidationAttribute { public string CompareProperty { get; set; } public EqualToPropertyAttribute(string compareProperty) { CompareProperty = compareProperty; ErrorMessage = string.Format(Messages.EqualToError, compareProperty); } public override bool IsValid(object value) { if (value == null) { return true; }

Conditionally validating portions of an ASP.NET MVC Model with DataAnnotations?

拟墨画扇 提交于 2019-11-30 13:21:24
问题 I have certain panels on my page that are hidden under certain circumstances. For instance I might have a 'billing address' and 'shipping address' and I dont want to validate 'shipping address' if a 'ShippingSameAsBilling' checkbox is checked. I am trying to use the new DataAnnotations capabilities of ASP.NET MVC 2 (preview 1) to achieve this. I need to prevent validation of the 'shipping address' when it is not displayed and need to find the way way to achieve this. I am talking mainly

DisplayFormat.DataFormatString for a phone number or social security number

折月煮酒 提交于 2019-11-30 12:45:25
Is there a way I can use the DisplayFormat attribute on a view model property to apply a DataFormatString format for a social security number or a phone number? I know I could do this with javascript, but would prefer to have the model handle it, if possible. [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "???????")] public string Ssn { get; set; } The following should work, however notice the type difference for the Ssn property. [DisplayFormat(DataFormatString = "{0:###-###-####}")] public long Phone { get; set; } [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString

MVC data annotations range validation not working properly

随声附和 提交于 2019-11-30 11:22:18
I have a RangeValidator on a property in my model to only allow Integers that are between 0 and 100. I have a partial view that displays a form to update the property via a jQuery UI dialog. I have looked at the source and can confirm that the data annotation attributes are being generated properly. However, the validatation isn't working properly. It does perform some kind of validation, but it isn't using the range I'm setting. The values 1, 10, and 100 do not produce the error. Any other single or two digit value produces the error. However, if I pad with zeros, all values less than one

Custom ValidationAttribute test against whole model

孤者浪人 提交于 2019-11-30 09:55:48
问题 I know this is probably not possible but let's say I have a model with two properties. I write a ValidationAttribute for one of the properties. Can that VA look at the other property and make a decision? So; public class QuickQuote { public String state { get; set; } [MyRequiredValidator(ErrorMessage = "Error msg")] public String familyType { get; set; } So in the above example, can the validator test to see what's in the "state" property and take that into consideration when validating

How to bind view model property with different name

戏子无情 提交于 2019-11-30 08:38:17
Is there a way to make a reflection for a view model property as an element with different name and id values on the html side. That is the main question of what I want to achieve. So the basic introduction for the question is like: 1- I have a view model (as an example) which created for a filter operation in view side. public class FilterViewModel { public string FilterParameter { get; set; } } 2- I have a controller action which is created for GETting form values(here it is filter) public ActionResult Index(FilterViewModel filter) { return View(); } 3- I have a view that a user can filter

ASP.NET MVC ModelMetaData: Is there a way to set IsRequired based on the RequiredAttribute?

旧时模样 提交于 2019-11-30 08:36:22
Brad Wilson posted a great blog series on ASP.NET MVC's new ModelMetaData: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-2-modelmetadata.html In it, he describes how the ModelMetaData class is now exposed in the Views and templated helpers. What I'd like to do is display an asterisk beside a form field label if the field is required, so I thought about using the IsRequired property of ModelMetaData. However, IsRequired by default is true for all non-nullable properties, while it's false for all nullable properties. The problem is, strings are always nullable, so the

Using ASP.Net MVC Data Annotation outside of MVC

心已入冬 提交于 2019-11-30 08:00:26
问题 i was wondering if there is a way to use ASP.Net's Data annotation without the MVC site. My example is that i have a class that once created needs to be validated, or will throw an error. I like the data annotations method, instead of a bunch of if blocks fired by the initaliser. Is there a way to get this to work? I thought it would be something like: Add data annotations Fire a method in the initialiser that calls the MVC validator on the class any ideas? i must admit i havent added the MVC

ASP.Net MVC 3 - Client side unobtrusive validation for Editor Templates

萝らか妹 提交于 2019-11-30 07:24:05
I am new to ASP.Net MVC 3, facing some issues while trying to implementing client side unobtrusive validation for a editor template I have created for showing date in a custom way. UI I need to show date in a three texbox UI format as I have put up a EditorTemplate for displaying the date in three parts as @model DateTime? <table class="datetime"> <tr> <td>@Html.TextBox("Day", (Model.HasValue ? Model.Value.ToString("dd") : string.Empty)) </td> <td class="separator">/</td> <td>@Html.TextBox("Month", (Model.HasValue ? Model.Value.ToString("MM") : string.Empty))</td> <td class="separator">/</td>

When using Data Annotations with MVC, Pro and Cons of using an interface vs. a MetadataType

家住魔仙堡 提交于 2019-11-30 07:02:31
If you read this article on Validation with the Data Annotation Validators , it shows that you can use the MetadataType attribute to add validation attributes to properties on partial classes. You use this when working with ORMs like LINQ to SQL, Entity Framework, or Subsonic. Then you can use the "automagic" client and server side validation. It plays very nicely with MVC. However, a colleague of mine used an interface to accomplish exactly the same result. it looks almost exactly the same, and functionally accomplishes the same thing. So instead of doing this: [MetadataType(typeof