Tag: 模型validation

ASP.NET Web API:如果从资源设置错误消息,则模型有效

问题是在ApiController中,如果我使用.rsx文件(参考资料)提供自定义错误消息,则ModelState.IsValid始终为true 。 这是我的模型: public class LoginModel { public string Email { get; set; } [Required] [MinLength(5)] public string Password { get; set; } } ApiController中的方法: [HttpPost] [ModelValidationFilter] public void Post(LoginModel model) { var a = ModelState.IsValid; } filter: public class ModelValidationFilterAttribute : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext) { if (actionContext.ModelState.IsValid == false) { […]

MVC .Net核心模型validation – 值”无效。 错误

我试图在MVC .Net Core中使用模型validation,并且无法管理替换此默认错误消息’值”无效’。 理论上,我们可以通过在模型中使用ErrorMessage Annotation来替换我们自己的自定义错误消息。 但我找不到办法使这个工作。 我的模特 [Required(ErrorMessage = “Date Required”)] [DataType(DataType.Date, ErrorMessage = “Invalid Date Format”)] [Display(Name = “Appointment Date”)] [DisplayFormat(DataFormatString = “{0:dd/MM/yyyy}”, ApplyFormatInEditMode = true)] public DateTime AppointmentDate { get; set; } 我为Required和DataType标记添加了不同的ErrorMessage ,如上所示。 我的HTML视图 你能帮我解决一下如何更换错误信息吗? 谢谢。

unit testing中的模型状态validation

我正在为这样的控制器编写unit testing: public HttpResponseMessage PostLogin(LoginModel model) { if (!ModelState.IsValid) return new HttpResponseMessage(HttpStatusCode.BadRequest); } 该模型看起来像: public class LoginModel { [Required] public string Username { set; get; } [Required] public string Password { set; get; } } 然后我有像这样的unit testing: [TestMethod] public void TestLogin_InvalidModel() { AccountController controller = CreateAccountController(); … var response = controller.PostLogin(new LoginModel() { }); Assert.AreEqual(HttpStatusCode.BadRequest, […]

数组必须包含1个元素

我有以下课程: public class CreateJob { [Required] public int JobTypeId { get; set; } public string RequestedBy { get; set; } public JobTask[] TaskDescriptions { get; set; } } 我想在TaskDescriptions上面有一个数据注释,以便数组必须包含至少一个元素? 很像[Required] 。 这可能吗?

日期的MVC模型validation

MVC 5是否有默认validation,我可以设置日期的最小值和最大值? 在我的模型中,我想要日期validation public class MyClass { [Required(ErrorMessage=”Start date and time cannot be empty”)] //validate:Must be greater than current date [DataType(DataType.DateTime)] public DateTime StartDateTime { get; set; } [Required(ErrorMessage=”End date and time cannot be empty”)] //validate:must be greater than StartDate [DataType(DataType.DateTime)] public DateTime EndDateTime { get; set; } } Ps:根据这个Asp.Net网站 ,使用Rangevalidation器进行DateTime时出现问题,不推荐使用。 注意:jQueryvalidation不适用于Range属性和DateTime。 例如,以下代码将始终显示客户端validation错误,即使日期在指定范围内: [Range(typeof(DateTime), “1/1/1966”, […]