data-annotations

Data-annotation getting errormessage out database

老子叫甜甜 提交于 2019-12-13 01:36:19
问题 With asp.net mvc you can use the annotation [Required (errormessage="This is required")] How can I create something like this: [Required (errormessage="ERRORXX")] So I can look up in a database what this ERRORXX is and display it on my form. Now my form displays ERRORXX. How can I create something that solves my problem? Thx! 回答1: Just an idea: why not pull the error messages from a resx file? I think this is the common way of doing this. It even allows you to localize your error messages

Which approach is better to define Data Annotation (View Model or Entity )? [closed]

陌路散爱 提交于 2019-12-12 20:05:34
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . Better approach to define Data annotation in entity or view model ? I have entity and view model for the same entity and override the class with Data Annotation of entity framework. The question is i can add them without creating a separate model however i have concern about

ASP.NET Web API: model is valid if error message is set from resources

和自甴很熟 提交于 2019-12-12 15:24:23
问题 The problem is that in ApiController ModelState.IsValid is always true if I use .rsx file (Resources) to provide custom error message. Here is my model: public class LoginModel { public string Email { get; set; } [Required] [MinLength(5)] public string Password { get; set; } } Method in ApiController: [HttpPost] [ModelValidationFilter] public void Post(LoginModel model) { var a = ModelState.IsValid; } And the filter: public class ModelValidationFilterAttribute : ActionFilterAttribute { public

MVC3 localization for default validation errors

时间秒杀一切 提交于 2019-12-12 13:23:21
问题 When I put [Required] attribute on my ViewModel's property MVC3 automatically generate error messages like: The Price field is required. My site's single language is Russian, so I want to have localized error messages. I can localize field's name with [Display(Name = "blablabla")] , but how can I localize the field is required part? Update: I know, that I can change an error message for concrete field by specifying it [Required(ErrorMessage = "blablabla")] , is there a way I can change it in

Any way to style text contained in DataAnnotation Attribute [Display(Name = “Text”)]?

假装没事ソ 提交于 2019-12-12 10:27:27
问题 I want to do something like this: [Display(Name = "Plain text. <span class=\"red strong\">Red text bolded.</span>")] Is this possible (to style the text within the Display Attribute)? Currently it is just displaying the literal text. 回答1: Is this possible (to style the text within the Display Attribute)? The problem doesn't lie within the [Display] attribute. It lies within the Html.LabelFor helper which you used to display. This attribute always HTML encodes the value. If you don't like this

Problem with DataAnnotations in partial class

三世轮回 提交于 2019-12-12 09:09:04
问题 So in my mvc project's Project.Repository I have [MetadataType(typeof(FalalaMetadata))] public partial class Falala { public string Name { get; set; } public string Age { get; set; } internal sealed class FalalaMetadata { [Required(ErrorMessage="Falala requires name.")] public string Name { get; set; } [Required(ErrorMessage = "Falala requires age.")] public string Age { get; set; } } } I use Falala as a model in my Project.Web.AccountControllers, and use a method to get violations.

Compare Email Address entered to database with DataAnnotations

吃可爱长大的小学妹 提交于 2019-12-12 08:56:14
问题 I have a class in my model in MVC: public class NewModel { public bool AllComplexes { get; set; } public int UserID { get; set; } public int? RoleID { get; set; } public int ComplexID { get; set; } [Required(ErrorMessage = "Please enter a user name."), StringLength(50)] public string Username { get; set; } [Required(ErrorMessage = "Please enter Password"), StringLength(100, ErrorMessage = "Password cannot be longer than 100 characters")] public string Password { get; set; } [Compare("Password

Entity Framework 4.1 Code First - Define many-to-many using data annotations only

﹥>﹥吖頭↗ 提交于 2019-12-12 08:34:25
问题 Is it possible to define a many-to-many relationship in Entity Framework 4.1 (Code First approach) using Data Annotations only, without model builder? For example, something like: Product = { Id, Name, ... } Category = { Id, Name, ... } ProductCategory = { ProductId, CategoryId } You get the picture. I don't want to have an intermediate entity ProductCategory in the context with two many-to-ones since I don't have any additional data, just the two FKs. Also, I should be able to define table

How to hide Entity Framework entity properties from strongly typed views?

狂风中的少年 提交于 2019-12-12 08:06:58
问题 I am using Entity Framework in my ASP.NET MVC 4.0 application and I want to know how to prevent or hide fields from my entity from being generated in my strongly typed view? Right now several primary key fields and timestamp fields are being generated on the view which I do not want. I know setting the property to internal as opposed to public works but I am not sure of the total downstream effect this will have. I prefer to use data annotations on the properties but the ones I have tried

MVC3: How to change the generic [Required] validation message text?

北慕城南 提交于 2019-12-12 07:57:31
问题 When you decorate a model object's property with the Required attribute and don't specify ErrorMessage or ResourceType/Name you get the validation message in the interpolated form of "The {0} field is required.", where param 0 is the value of the DisplayName attribute of that property. I want to change that default string to something else but I want to keep the generic nature of it, that is I don't want to specify ErrorMessage or ResourceType/Name for every property of the model object.