data-annotations

ASP.Net MVC DisplayFormat

﹥>﹥吖頭↗ 提交于 2019-11-26 22:42:44
问题 In my model I have the following DataAnnotations on one of my properties [Required(ErrorMessage = "*")] [DisplayFormat(DataFormatString = "{0:d}")] [DataType(DataType.Date)] public DateTime Birthdate { get; set; } The required annotation works great, I added the other 2 to try and remove the time. It gets bound to an input in the view using <%=Html.TextBoxFor(m => m.Birthdate, new { @class = "middle-input" })%> However whenever the view loads I still get the time appearing in the input box.

client-side validation trips on DataAnnotation Range attribute

本小妞迷上赌 提交于 2019-11-26 22:25:30
问题 I have the following code in my Model class: [Range(1, 100)] public decimal Price { get; set; } After recent upgrade (I assume) of jquery.validate to 1.11.0, I am getting an error even if I enter valid value. If I turn off client validation in web.config - works fine. All other attributes (StringLength, Required) work fine. Generated HTML is the following (line breaks added for clarity): <input class="text-box single-line" data-val="true" data-val-number="The field Price must be a number."

IValidatableObject Validate method firing when DataAnnotations fails

為{幸葍}努か 提交于 2019-11-26 22:23:03
问题 I've a ViewModel which has some DataAnnotations validations and then for more complex validations implements IValidatableObject and uses Validate method. The behavior I was expecting was this one: first all the DataAnnotations and then, only if there were no errors, the Validate method. How ever I find out that this isn't always true. My ViewModel (a demo one) has three fileds one string , one decimal and one decimal? . All the three properties have only Required attribute. For the string and

MVC Validation Lower/Higher than other value

て烟熏妆下的殇ゞ 提交于 2019-11-26 22:08:52
How is the best way to validate a model in MVC.Net where I want to accept a minimum/maximum. Not individual min/max values for a field. But separate fields for a user to specify a minimum/maximum. public class FinanceModel{ public int MinimumCost {get;set;} public int MaximumCost {get;set;} } So I need to ensure that MinimumCost is always less than Maximum cost. You can use a custom validation attribute here is my example with dates. But you can use it with ints too. First, here is the model : public DateTime Beggining { get; set; } [IsDateAfterAttribute("Beggining", true,

Remote Validation in ASP.Net MVC 3: How to use AdditionalFields in Action Method

狂风中的少年 提交于 2019-11-26 22:08:18
I've been using the new ASP.Net MVC 3 RemoteAttribute to send a remote call to an action method that had a single parameter. Now I want to pass in a second parameter using the AdditionalFields property: [Remote("IsEmailAvailable", "Users", AdditionalFields = "InitialEmail")] Where IntialEmail is a hidden field in the view. The action looks like so: public JsonResult IsEmailAvailable( string email, string InitialEmail) { //etc. } When the view is rendered, the hidden field is populated, but when the Action method is triggered remotely, the value is an empty string. I've seen elsewhere case

Validate object based on external factors (ie. data store uniqueness)

孤街浪徒 提交于 2019-11-26 22:02:58
Description My solution has these projects: DAL = Modified Entity Framework DTO = Data Transfer objects that are able to validate themselves BL = Business Layer Services WEB = presentation Asp.net MVC application DAL, BL and WEB all reference DTO which is great. The process usually executes this way: A web request is made to the WEB WEB gets DTOs posted DTOs get automagically validated via custom ActionFilter validation errors are auto-collected (Validation is OK) WEB calls into BL providing DTOs BL calls into DAL by using DTOs (can either pass them through or just use them) DTO Validation

How can I access the DisplayName data annotation value from code?

风流意气都作罢 提交于 2019-11-26 21:57:43
问题 public static string ProductHelper(this Product p) { // Need to get the DisplayName value for p.Name property } EDIT: [MetadataType(typeof(ProductMetadata))] public partial class Product { public class ProductMetadata { [DisplayName("Product name")] public object Name { get; set; } } } 回答1: Type type = typeof(Product); DisplayNameAttribute att = (DisplayNameAttribute)type.GetProperty("Name").GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault(); This assumes the attribute

Assign format of DateTime with data annotations?

假装没事ソ 提交于 2019-11-26 21:47:58
I have this attribute in my view model: [DataType(DataType.DateTime)] public DateTime? StartDate { get; set; } If I want to display the date, or populate a textbox with the date, I have these: <%: Model.StartDate %> <%: Html.TextBoxFor(m => m.StartDate) %> Whenever the date is displayed, it's displayed like: 01/01/2011 12:00:00 AM But I'd like to only display 01/01/2011 Is there a way to apply a display format with data annotations? I don't want to have to go to every instance where I display a date, and add some code to format it. Try tagging it with: [DisplayFormat(ApplyFormatInEditMode =

How can I tell the Data Annotations validator to also validate complex child properties?

陌路散爱 提交于 2019-11-26 20:23:11
Can I automatically validate complex child objects when validating a parent object and include the results in the populated ICollection<ValidationResult> ? If I run the following code: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace ConsoleApplication1 { public class Person { [Required] public string Name { get; set; } public Address Address { get; set; } } public class Address { [Required] public string Street { get; set; } [Required] public string City { get; set; } [Required] public string State { get; set; } } class Program { static

What is use of UIHint attribute in MVC [duplicate]

感情迁移 提交于 2019-11-26 20:19:24
问题 This question already has an answer here: UIHint Attribute in MVC 2 answers Can anyone please explain me what is the use of UIHint attribute in MVC . Why do we need this. and when and how to use . Thanks 回答1: 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