简体   繁体   中英

EF: Restriction on data types mapping

Is it possible to edit the error message of type of data on EF ?

For example, I've to display to the end-user that the value entered on ID's field must be integer .

public int ID { get; set; }

Thanks a lot !!

You can use Range validator:

 [Range(int.MinValue, int.MaxValue, ErrorMessageResourceName = "IDMustBeInteger",
    ErrorMessageResourceType = typeof (Resources))]

You can use a regular expression annotation to validate digits only

[RegularExpression(@"^\d+$",ErrorMessage="the value entered on ID's field must be integer")]
public int MyInt { get; set; }

在此输入图像描述

It seems that you have two possible solutions for your problem:

  1. You can write client validation to check this on client (using regexp or something else)

  2. You can use custom ModelBinder to intercept default error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM