data-annotations

TDD: What is best practice to test DataAnnotations in ASP.NET MVC 3?

可紊 提交于 2019-12-02 23:00:59
I'm participating in a project using ASP.NET MVC 3 and DataAnnotations. We have DataAnnotations in ViewModels classes. How do you write unit tests for these validations? ViewModel example: public class AchievementVM { [Required(ErrorMessage = "The title field is required.")] [StringLength(100, ErrorMessage = "Title must be 100 characters or less.")] public string Title { get; set; } } Thanks! Phil Patterson The .NET framework comes with a Validator class which can exercise your validation logic in isolation. The code to test could look like this: var achievement = new AchievementVM(); var

Compare (password) attribute

时间秒杀一切 提交于 2019-12-02 22:34:19
I'd like to create a view model for a new user using the code below. The "User" class contains just the two properties (simplified for now) that I will persist to the database; the view model adds a "compare password" field, which is only used in the view. I'd prefer to have the view model use the "User" class directly, rather than repeating all of the fields defined in "User". My question is how do I properly reference "User.Password" in the [Compare] attribute for the "ComparePassword" field? public class User { [Required] public string UserName { get; set; } [Required] [DisplayName(

Conditional data annotation

烂漫一生 提交于 2019-12-02 22:18:47
Is there a way to make a data annotation conditional? I have a table Party where I store both organisations and persons. If I'm adding an organisation I don't want the field surname to be required, but only if I'm adding a person. public class Party { [Required(ErrorMessage = "{0} is missing")] [DisplayName("Your surname")] public object surname { get; set; } [DisplayName("Type")] public object party_type { get; set; } ... } I'd like a condition for the required data annotation of surname, something like: if (party_type=='P') then surname is required, else the surname can be empty. EDIT If I

How can I create a generic UniqueValidationAttribute in C# and DataAnnotation?

穿精又带淫゛_ 提交于 2019-12-02 21:27:32
I'm trying to create a UniqueAttribute using the System.ComponentModel.DataAnnotations.ValidationAttribute I want this to be generic as in I could pass the Linq DataContext, the table name, the field and validate if the incoming value is unique or not. Here is a non-compilable code fragment that I'm stuck at right now: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel.DataAnnotations; using System.Data.Linq; using System.ComponentModel; namespace LinkDev.Innovation.Miscellaneous.Validation.Attributes { public class UniqueAttribute

Validation using attributes

六眼飞鱼酱① 提交于 2019-12-02 20:50:36
I have, let's say, this simple class: public class User { [Required(AllowEmptyStrings = false, ErrorMessage="EmailIsRequired"] public string EmailAddress { get; set; } } I know how to use Validator.TryValidateProperty and Validator.TryValidateObject in the System.ComponentModel.DataAnnotations namespace. In order for this to work, you need an actual instance of the object you want to validate. But now, I want to validate a certain value without an instance of the User class, like: TryValidateValue(typeof(User), "EmailAddress", "test@test.com"); The goal is that I want to test a value before

ASP.NET-MVC 2 DataAnnotations StringLength

▼魔方 西西 提交于 2019-12-02 20:15:02
Can I use the MVC 2 DataAnnotations to specify a minimum length for a string field? Has anyone done this or have they created custom attributes and if so do you mind sharing the source? If you're using asp.net 4.0, you can use the StringLength attribute to specify a minimum length. Eg: [StringLength(50, MinimumLength=1)] public string MyText { get; set; } Use a regular expression attribute. These are interpreted on the client side as well. [RegularExpression(Regexes.MinStringLength)] public string MyText { get; set; } Where Regexes.MinStringLength is a static regular expression class. Inline

ASP.NET MVC4 Multi-lingual Data Annotations

ぐ巨炮叔叔 提交于 2019-12-02 19:38:09
In a standard application I have the following: [Required] [DisplayName("Email Address")] public string EmailAddress { get; set; } ...this in turn generates a label for this form field automatically in English. Now, if I need my app to support 5 languages, what is the best approach from a ASP.NET MVC application to handle this? Scope of application is about 400 - 600 data fields. UPDATE: I will also require support for updating small sections of text in the application like page names and introductions to each form (small paragraph). smartcaveman Instead of assigning the actual values to the

Web api validation on nullable parameter fails

你。 提交于 2019-12-02 18:13:40
问题 I am trying to validate very simple method and I am getting The value 'null' is not valid for Nullable`1 error. [ValidateModel] public IEnumerable<SomeData> Get(bool? showExtra = null) { return this.MockDataManager.ShowData(showExtra); } ValidateModel property is: public override void OnActionExecuting(HttpActionContext actionContext) { if (actionContext != null && actionContext.ModelState.IsValid == false) { actionContext.Response = actionContext.Request.CreateErrorResponse( HttpStatusCode

Data Annotation for currency format not working

旧巷老猫 提交于 2019-12-02 17:43:31
问题 In my ASP.NET MVC Core web project on VS2015 , the following model is displaying data as, e.g., 15481 instead of $15,481 even though I'm using [DisplayFormat] below: Models : public class State { [Key] public int StateId { get; set; } [Column(TypeName ="varchar(40)")] public string StateName { get; set; } [Column(TypeName = "char(2)")] public string StateCode { get; set; } } public class Sales { [Key] public int SalesId { get; set; } public int? FiscalYear { get; set; } [DisplayFormat

DisplayName metadata does not show up on view

大城市里の小女人 提交于 2019-12-02 17:07:36
问题 How will I show the correct DsiplayName on my view considering the following model. <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Project.Models.RegisterViewModel>" %> <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> <% using (Html.BeginForm()) {%> <table> <tr> <td class="label"> <%: Html.LabelFor(model => model.User.UserPrimaryEmail)%> </td> <td class="field"> <%: Html.TextBoxFor(model => model.User