data-annotations

How to add a new line into Display Attribute, Name field

被刻印的时光 ゝ 提交于 2019-12-02 14:38:33
问题 I'm working on an MVC3 application and am using data attributes for the display name fields on the screen. Below is a representative sample - [Required] [Display(Name = "Staff Id (format \"9999\")")] [StringLength(10)] [UIHint("StaffId")] public string StaffId { get; set; } What I would like to do is to have the name display on two lines with line break right after "Id" text. So it would display as Staff Id (format "9999") Is there a way to do this? 回答1: [Required] [Display(Name = "Staff Id

Data Annotations, IDataErrorInfo and MVVM

只愿长相守 提交于 2019-12-02 13:40:24
I'm trying to find the best way to validate data in MVVM. Currently, I'm trying to use IDataErrorInfo with Data Annotations using the MVVM pattern. However, nothing seems to work and I'm not sure what I could be doing wrong. I have something like this. Model public class Person : IDataErrorInfo { [Required(ErrorMessage="Please enter your name")] public string Name { get; set; } public string Error { get { throw new NotImplementedException(); } } string IDataErrorInfo.this[string propertyName] { get { return OnValidate(propertyName); } } protected virtual string OnValidate(string propertyName)

Data Annotation for currency format not working

南楼画角 提交于 2019-12-02 11:59:43
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(DataFormatString = "{(0:C0)}")] public float? SaleAmount { get; set; } public int StateId { get; set; } public

DisplayName metadata does not show up on view

大兔子大兔子 提交于 2019-12-02 07:41:32
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.UserPrimaryEmail)%> </td> <td class="field-error"> <div class="field-error-msg"> <%: Html

Web api validation on nullable parameter fails

蹲街弑〆低调 提交于 2019-12-02 07:37:51
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.BadRequest, actionContext.ModelState); } } Now, if i call method with /true and /false it works. Also it

How can I ignore missing fields from the MetadataType attribute?

烈酒焚心 提交于 2019-12-02 05:14:13
I have DTOs that are mapped to ViewModels. To avoid having to manage validation attributes (and other attributes), I wanted to write the validation attributes for all the properties on a single class and reuse it on my ViewModels. However, when I try to use the Metadata on a ViewModel that does not have all the properties of the DTO (all of them really...), it gives me an System.InvalidOperationException exception. Exception: Le type de métadonnées associé pour le type 'MyProject.EntityViewModel' contient les propriétés ou champs inconnus suivants : AnotherProperty. Vérifiez que les noms de

Custom Model Validator for Integer value in ASP.NET Core Web API

情到浓时终转凉″ 提交于 2019-12-02 04:01:56
问题 I have developed a custom validator Attribute class for checking Integer values in my model classes. But the problem is this class is not working. I have debugged my code but the breakpoint is not hit during debugging the code. Here is my code: public class ValidateIntegerValueAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value != null) { int output; var isInteger = int.TryParse(value.ToString(), out

ef5 database first data annotation

隐身守侯 提交于 2019-12-01 22:32:24
问题 I am starting MVC4 with VS2012. I am also using EF5 with the "Database First" method of creating my classes. However because the generated glasses can be regenerated I cannot put the Data Annotation details to assist with validation. I have seen some code snippets that use MetaData and partial classes but I was wondering if anyone knows of a small compilable example that I can look at and pull apart to better understand how the vasious classes interlink. Many many thanks for any help. Dave

Date range validation with Entity Framework 4 data annotations

喜欢而已 提交于 2019-12-01 22:30:41
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}", ApplyFormatInEditMode = true)] [DataType(DataType.Date)] [Range(typeof(DateTime), "01/01/1900", "06/06

Data Annotation/Validation and dynamic values

只愿长相守 提交于 2019-12-01 20:44:18
问题 If some of my models have dynamic validation conditions (i.e. the string length can be minimum of 8 or 12 depending on a database value or some other dynamic value) is it impossible to use data annotation for validation? From what I understand, the values of any parameter (example StringLength min/max value) have to be truly static. Are there alternatives for applications that have dynamic validation values? 回答1: Your pretty much stuck with writing your own custom validationattribute: http:/