Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

How to translate the default ModelErrors in ASP.NET Core?

In my app, I have custom ModelErrors set, with a fixed message in my case in spanish, for example:

if (tripOdometer <= 0)
{
    ModelState.AddModelError("Odometer", $"Inserte una distancia superior a la última almacenada, que fue de {lastFuelingEntryOdometer}");
    return View(request);
}

But my problem is that the errors that come with DataAnnotations package are in english (logically), and produce in my case a spanglish output.

A property in one of my models:

[Required]
[Range(1970, 2021)]
[Display(Name = "A?o de Fabricación")]
public int Year { get; set; }

OUTPUTS: The A?o de Fabricación field is required. The field A?o de Fabricación must be between 1970 and 2021.

Is it possible to translate them, in a general way for all the models and properties?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...