data-annotations

null values from listbox, are not evaluated in the model binding of ASP.NET-MVC

别等时光非礼了梦想. 提交于 2019-12-25 06:39:22
问题 The model validation doesn't evaluates the attributes linked to listbox values if you don't select at least one of them. This way is not possible to do a model evaluation using DataAnnotations in order to inform required values. The controller: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using TestValidation.Models; namespace TestValidation.Controllers { [HandleError] public class HomeController : Controller { private SelectList

Having Range attribute and one extra number

雨燕双飞 提交于 2019-12-25 06:33:57
问题 I have a property with RangeAttribute.. let's say: [Range(0, 30, ErrorMessageResourceName = "Range", ErrorMessageResourceType = typeof(validationMessages)))] public int? years {get; set;} I want to have the validation range but i also want to let the user enter ONE other number let's say 66 as well. is there anyway to have an exception here? i mean if the user enters 44, the error is shown but if he/she enters 66 (only) he/she does not get any error? 回答1: You need to define your own

DataAnnotations driving client side validation issue

你说的曾经没有我的故事 提交于 2019-12-25 04:35:17
问题 I am trying to get what I think is a simple example of using DataAnnotations on a model to drive the client side validation as well. Here is my model... public class Person { [Required(ErrorMessage = "First Name Required")] public string FirstName { get; set; } [Required(ErrorMessage = "Last Name Required")] public string LastName { get; set; } } Here is my controller... public class FriendsController : Controller { public ActionResult Create() { Person newFriend = new Person(); return View

MVC Custom dataannotation

自古美人都是妖i 提交于 2019-12-25 03:38:11
问题 i create two custom dataannotation for this class as follow public class users { public int Id { set; get; } [Required(ErrorMessage = "username is Required")] [usernameValidation(ErrorMessage= "Sorry this name is already exist")] // [MaxLength(ma)] public string username { set; get; } [Required(ErrorMessage = "Password is required")] [DataType(DataType.Password)] public string password { set; get; } [Required(ErrorMessage = "Confirm Password is required")] [StringLength(255, ErrorMessage =

ASP.Net MVC2 - Set ViewModel values with retrieved from db object's values with DataAnnotations

一世执手 提交于 2019-12-25 03:27:54
问题 Prior to using a ViewModel, I could easily pass the "soon to be edited" object directly to the view without needing to fuss over setting individual properties etc as the View conveniently accepted the Employee type directly.. [HttpGet] public ActionResult EditEmployee(int? id) { EmployeeRepository ER = new EmployeeRepository(); Employee SomeEmployee = ER.GetEmployee(id.Value); if(SomeEmployee!=null) return View(SomeEmployee); But now I'm using a ViewModel with DataAnnotations attributes

Asp.Net MVC 3.0 Model Localization With RegularExpression Attribute

老子叫甜甜 提交于 2019-12-24 17:08:19
问题 I've written a custom error message localization logic in my custom DataAnnotationsModelMetadataProvider class. It's working just fine with build-in StringLengthAttribute or RequiredAttribute validation error messages. But i am having trouble with my custom derived RegularExpressionAttribute classes. The logic i am using is something like below : public class AccountNameFormatAttribute : RegularExpressionAttribute { public AccountNameFormatAttribute() : base(Linnet.Core.Shared.RegExPatterns

Asp.Net MVC 3.0 Model Localization With RegularExpression Attribute

半腔热情 提交于 2019-12-24 17:04:15
问题 I've written a custom error message localization logic in my custom DataAnnotationsModelMetadataProvider class. It's working just fine with build-in StringLengthAttribute or RequiredAttribute validation error messages. But i am having trouble with my custom derived RegularExpressionAttribute classes. The logic i am using is something like below : public class AccountNameFormatAttribute : RegularExpressionAttribute { public AccountNameFormatAttribute() : base(Linnet.Core.Shared.RegExPatterns

ASP.NET MVC - Choose which validation annotations to use

99封情书 提交于 2019-12-24 15:29:40
问题 I have a model with properties that look like this: public class YourDetails { [Required(ErrorMessage = "Code is required")] [StringLength(10, ErrorMessage = "Code length is wrong", MinimumLength = 2)] [Range(0, int.MaxValue, ErrorMessage = "Please enter a value bigger than {1}")] public int Code { get; set; } } The UI validation is setup the usual out of the box way with unobtrusive JS validation plugin. The issue: I have 2 navigation actions, back and next. Next is fine, validation fires

Regular expression for 5 comma separated email id

拟墨画扇 提交于 2019-12-24 12:15:05
问题 I trying to validate 5 comma separated email id in one regular expression. I currntly using below regex ^([\w+-.%]+@[\w-.]+\.[A-Za-z]{2,4},?)+$ This is valid for one email id. I would like to know how I can achieve the same, any small inputs on the same is also greatly appreciated. Thanks in advance. 回答1: First of all, fix the pattern: - in between two chars inside a character class forms a range. So, the email part of your regex should be [-\w+.%]+@[\w-.]+\.[A-Za-z]{2,4} (note the position

ASP.NET MVC 5: EmailAddress attribute custom error message

こ雲淡風輕ζ 提交于 2019-12-24 07:41:33
问题 In register form I use EmailAddress attribute to validate user email. public class RegisterViewModel { [Required(ErrorMessage = "Pole wymagane")] [Display(Name = "Email")] [DataType(DataType.EmailAddress)] [EmailAddress] public string Email { get; set; } } Is there any chance to show what is wrong with email address if validation fails? For example 'oops, I see that your email address contains whitespace' 回答1: You have to add another validation for that. Example using [RegularExpression]