data-annotations

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

☆樱花仙子☆ 提交于 2019-12-20 10:44:11
问题 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! 回答1: The .NET framework comes with a Validator class which can exercise your validation logic in

Client form validation not working with modal dialog in MVC

北慕城南 提交于 2019-12-20 09:47:11
问题 I am changing a create form to become a modal dialog and jquery Unobtrusive validation stops working and don't know how to fix it. Index.cshtml has a link to trigger a modal dialog. <a href="#" id="createCustomer">Create</a> @section scripts{ <script type="text/javascript"> $('#createCustomer').on('click', function () { $.get('/Customer/Create', function (data) { $('#modalContainer').html(data); $('#myModal').modal({}); }); }); Create.cshtml is a partial view. @model Demo.Web.Models

Date range validation with Entity Framework 4 data annotations

寵の児 提交于 2019-12-20 02:49:22
问题 I am using Entity Framework 4 to provide the model for a ASP.NET MVC3 / Razor2 web application. I am using DataAnnotations to implement validation. I need to limit some dates to the range accepted by the SQL smalldatetime type. My problem is that I can't get the RangeAttribute to work correctly for a date field. The model metadata definition for the field in question is: [Display(ResourceType = typeof(Resources.Patient), Name = "DateOfBirth_Name")] [DisplayFormat(DataFormatString = "{0:d}",

MVC 3 client-side validation on collection with data annotations — not working

两盒软妹~` 提交于 2019-12-19 10:45:16
问题 I am trying to get client side validation to work on my model -- so far it's not working. My model has a property that's a collection: public class NewsEventsModel { public List<NewsItemDetails> newsItems { get; set; } public int pageNumber { get; set; } public int totalPages { get; set; } public bool canManageNews { get; set; } public long userID { get; set; } } and NewsItemDetails is defined thusly: public class NewsItemDetails { public long itemID { get; set; } public long postedByID { get

Custom DataAnnotations Validator Derived from RegularExpressionAttribute

ぃ、小莉子 提交于 2019-12-19 08:05:32
问题 The Gu provides an example of how you might create a custom validator that overrides RegularExpressionAttribute . The advantage of this is that you don't have to create a custom Model Validator but I can't get it to work. Given the following code: public class NameAttribute : RegularExpressionAttribute { public NameAttribute() : base(@"^[\w\s\-\']+$") { } } This works: [RegularExpression(@"^[\w\s\-\']+$")] But this doesn't: [Name] Have I misunderstood an aspect of Scott's example or is the

Get Data Annotations attributes from model

帅比萌擦擦* 提交于 2019-12-19 05:59:14
问题 I want to create custom client-side validator, but I want define validation rules via Data Annotations attributes at business logic layer. How can I access model validation attributes in runtime? I want to write 'generator', which will convert this code: public class LoginModel { [Required] [MinLength(3)] public string UserName { get; set; } [Required] public string Password { get; set; } } into this one: var loginViewModel= { UserName: ko.observable().extend({ minLength: 3, required: true })

Using an interface as the model type of a partial view + data annotations

喜你入骨 提交于 2019-12-18 22:32:49
问题 I have a case where a complex partial view needs different validation of fields depending on where the partial view is used. I thought I could get around this by making the partial view take an interface as the model type and implementing two different ViewModels based on the interface. The data annotations in the two ViewModels would be different. I would then supply an instance of the correct ViewModel to the partial view. But what I'm finding is that the only annotations that are

How do I use System.ComponentModel.DataAnnotations.AssociationAttribute

爷,独闯天下 提交于 2019-12-18 21:53:48
问题 Some time ago I asked this question: What is the purpose of each of the System.ComponentModel.DataAnnotations attributes? However, I had no luck with getting replies. This question was a bit broad in the sense that it asked for documentation about every dataannotation attribute. At this moment, I am mostly interested in the Association attribute. I am using ASP.NET MVC3 with Entity Framework 4 and would like to annotate my POCOs. I am using foreign keys in my POCOs (somehow feels wrong, but

ASP.NET MVC displaying date without time

梦想与她 提交于 2019-12-18 19:27:10
问题 I have my model field decorated in the following way: [DataType(DataType.Date)] [Display(Name = "Date of birth")] public string DateOfBirth { get; set; } When I want to display the value in the view using the following code: <%: Html.DisplayFor(m => m.DateOfBirth) %> The problem is that the date is displayed together with its time value. I wonder why it does not take DateType attribute into consideration and displays only the date value without time. I know that I may create a display

CustomValidationAttribute specifed method not being called

别说谁变了你拦得住时间么 提交于 2019-12-18 17:14:44
问题 I'm using the System.ComponentModel.DataAnnotations.CustomValidationAttribute to validate one of my POCO classes and when I try to unit test it, it's not even calling the validation method. public class Foo { [Required] public string SomethingRequired { get; set } [CustomValidation(typeof(Foo), "ValidateBar")] public int? Bar { get; set; } public string Fark { get; set; } public static ValidationResult ValidateBar(int? v, ValidationContext context) { var foo = context.ObjectInstance as Foo;