data-annotations

DataAnnotations or Application Validation block

浪尽此生 提交于 2020-01-02 03:36:12
问题 Whats the difference between DataAnnotations and Application Validation Block? 回答1: DataAnnotations is an attribute based model to 'annotate' your data and it is in the .NET framework itself. Its most obvious use is for validation, as ASP.NET MVC does for instance. Validation Application Block itself is a validation framework, created by the Microsoft P&P team, but it is not part of the .NET framework. It also contains attributes to 'annotate' your data and in its newest version (5.0) the

difference between: [ScaffoldColumn (false)] and [Display (AutoGenerateField = false)]

笑着哭i 提交于 2020-01-02 03:35:27
问题 To render HTML in my edit view, I use the helper @Html.EditorForModel() . My model: [Required(ErrorMessage = "Campo obrigatório")] [Display(Name = "Nome completo")] public string Name { get; set; } [Required(ErrorMessage = "Campo é obrigatório")] [StringLength(100, ErrorMessage = "A {0} deve ter pelo menos {2} characteres.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "Senha")] public string Password { get; set; } [DataType(DataType.Password)] [Display(Name = "Confirmar

DataAnnotations MaxLength and StringLength for string[] or List<string>?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-01 12:01:58
问题 I am trying to figure out if there is a proper way to achieve this with DataAnnotations: Have an array or List of strings where the maximum number of elements in the array or List is 2 items and where each string may only be, say 255 characters long. Will this work: [MaxLength(2)] [StringLength(255)] public string[] StreetAddress { get; set; } I would rather not have to make a new class just to hold a string Value property to constrain each string to 255 characters. 回答1: You can create your

Enable/Disable mvc server side validation dynamically

て烟熏妆下的殇ゞ 提交于 2020-01-01 06:34:26
问题 I have an mvc form with multiple submit buttons - 'Save Draft' and 'Publish'. The objective is to skip both client side(javascript/unobstructive) validation and server side validation when the 'Save Draft' button is clicked and the form is submitted. But I do need to trigger both validation if the 'Publish' button is clicked. My research has led me to few solutions. Client Side -By writing a jquery plugin (function ($) { $.fn.turnOffValidation = function (form) { var settings = form.validate(

Range and DisplayFormat Attributes on TimeSpan

老子叫甜甜 提交于 2020-01-01 06:27:08
问题 I am building a Scheduling Screen and need to display a Time field for users to enter the time of day for the schedule. I'm not sure if this is the best option, but I am using a TimeSpan for the field. To validate the input, I want to use the Range attribute and the DisplayFormat attribute. When I debug and enter a seeming valid value, the Range attribute indicates an out of range error. Can anyone see what I am doing wrong? Is TimeSpan the proper type for this usage? Any help is greatly

Get error message when using custom validation attribute

自闭症网瘾萝莉.ら 提交于 2020-01-01 05:41:12
问题 I'm using the CustomValidationAttribute like this [CustomValidation(typeof(MyValidator),"Validate",ErrorMessage = "Foo")] And my validator contains this code public class MyValidator { public static ValidationResult Validate(TestProperty testProperty, ValidationContext validationContext) { if (string.IsNullOrEmpty(testProperty.Name)) { return new ValidationResult(""); <-- how can I get the error message from the custom validation attribute? } return ValidationResult.Success; } } So how can I

Create Foreign Key using Data Annotations

馋奶兔 提交于 2020-01-01 05:18:12
问题 In the code below, I need to set a foreign key constrant on ParentInfoAddProperties.ParentQuestionAnswersId so that it is dependent on ParentQuestionAnswers.Id (which is a Primary Key). I am attempting to do so using data annotations but Entity Framework 6 wants to create a new Foreign Key column in my ParentQuestionAnswers table which references the ParentInfoAddProperties.Id column not the ParentInfoAddProperties.ParentQuestionAnswersId column. I do not want Entity Framework to create a new

ASP.NET MVC 3 - Data Annoation and Max Length/Size for Textbox Rendering

百般思念 提交于 2020-01-01 03:16:11
问题 I know on the Razor View file, we can do something like this @Html.TextBox("username", null, new { maxlength = 20, autocomplete = "off" }) However, I am hoping to create a model for the MVC that can be used to create a form with explicitly defined the size and max length of the textboxes. I try [StringLength(n)] on top of the properties of the model, but that seems to only do the validation ratherh set the size of the textbox. Is there anyway that we can define the length of the text field as

Web API nullable required property requires DataMember attribute

痴心易碎 提交于 2019-12-31 09:12:12
问题 I am receiving the following VM on a Web API Post action public class ViewModel { public string Name { get; set; } [Required] public int? Street { get; set; } } When I make a post I get the following error: Property 'Street' on type 'ViewModel' is invalid. Value-typed properties marked as [Required] must also be marked with [DataMember(IsRequired=true)] to be recognized as required. Consider attributing the declaring type with [DataContract] and the property with [DataMember(IsRequired=true)]

FileExtensions attribute of DataAnnotations not working in MVC

回眸只為那壹抹淺笑 提交于 2019-12-30 06:21:08
问题 I am trying to upload a file using HTML FileUpload control in MVC. I want to validate the file to accept only specific extensions. I have tried using FileExtensions attribute of DataAnnotations namespace, but its not working. See code below - public class FileUploadModel { [Required, FileExtensions(Extensions = (".xlsx,.xls"), ErrorMessage = "Please select an Excel file.")] public HttpPostedFileBase File { get; set; } } In the controller, I am writing the code as below - [HttpPost] public