C# ASP.NET MVC本地化或更改默认模型绑定错误消息
如何更改“值'some value'对'some property'无效”验证错误的语言? 有人能帮忙吗?我想把图片中的错误翻译成俄语。我读了很多网站,尝试使用C# ASP.NET MVC本地化或更改默认模型绑定错误消息,c#,asp.net-mvc,validation,localization,model-binding,C#,Asp.net Mvc,Validation,Localization,Model Binding,如何更改“值'some value'对'some property'无效”验证错误的语言? 有人能帮忙吗?我想把图片中的错误翻译成俄语。我读了很多网站,尝试使用RegularExpression,但没有帮助 可能是我不知道该怎么做 我只需要翻译错误,不需要改变文化 在web.config中: 我的看法是: @使用(Html.BeginForm()) { @Html.HiddenFor(m=>m.Id) /*其他一些属性*/ Среднее количество блокшотов @TextBo
RegularExpression
,但没有帮助
可能是我不知道该怎么做
我只需要翻译错误,不需要改变文化
在web.config中:
我的看法是:
@使用(Html.BeginForm())
{
@Html.HiddenFor(m=>m.Id)
/*其他一些属性*/
Среднее количество блокшотов
@TextBoxFor(m=>m.BlockPerGame,新的{@class=“form control”})
@Html.ValidationMessageFor(m=>m.BlockPerGame)
/*其他一些属性*/
Добавить
@ActionLink(“object=”Index“,null,新的{@class=“button-button-cancel”,id=“button-cancel”})
}
和我的控制器:
公共类AdminController:控制器
{
/*其他一些方法*/
[HttpPost]
公共异步任务编辑(播放器、字符串选择团队)
{
if(ModelState.IsValid)
{
if(ChoosingTeam!=string.Empty)
{
尝试
{
player.TeamId=int.Parse(选择团队);
wait repository.SavePlayerAsync(播放器);
TempData[“message”]=string.Format(播放器名称、播放器姓氏);
返回操作(“索引”);
}
捕获(异常exc)
{
控制台写入线(exc.Message);
}
}
}
IEnumerable list=new SelectList(repository.Teams,“Id”,“Name”);
ViewBag.ChoosingTeamName=列表;
返回视图(播放器);
}
}
为属性输入无效值时,如果模型绑定器无法将该值绑定到属性,则模型绑定器会为该属性设置错误消息。它不同于数据注释模型验证。这实际上是模型绑定器验证错误
本地化或更改默认模型绑定错误消息
模型绑定错误消息与模型验证消息不同。要自定义或本地化它们,您需要创建一个全局资源,并在DefaultModelBinder.ResourceClassKey
的Application\u Start
中注册它
为此,请执行以下步骤:
:PropertyValueInvalid
值“{0}”对{1}无效。
PropertyValueRequired:
需要一个值。
ErrorMessages.resx
中编写自定义消息,然后跳过下一步
xx xx
,例如波斯语的fa-IR
然后输入这些消息的翻译,例如ErrorMessages.fa-IR.resx:
:属性值无效
属性值无效
需要的属性值:
应用程序\u Start
中粘贴代码:
DefaultModelBinder.ResourceClassKey = "ErrorMessages";
对于ASP.NET Core,请阅读以下帖子:。您输入的浮点数无效(使用“,”istead of.”作为分隔符)。如果将字符串作为数据类型与[RegularExpression]一起使用,而不是使用float,会怎么样?通过这种方式,您可以添加自定义错误消息不翻译1)我创建ErrorMasseges.ru-ru.resx 2)名称:PropertyValueInvalid;值:1。名称:PropertyValueRequired;值:global.asax add DefaultModelBinder.ResourceClassKey=“ErrorMessages”中的值:3);我也有同样的英语错误,我试着翻译它,因为你的应用程序的文化仍然是中性文化。对你来说,因为我看到你在使用俄语错误消息,即使是在中性文化中,只要在
ErrorMasseges.resx
中使用俄语消息即可。忘记ErrorMasseges.ru.resx
。确保您已在App\u GlobalResources
中创建了资源文件,并遵循我之前的评论。据我所知,您没有对验证属性使用本地化,所以一个用于中性区域性的资源文件就足够了。我的错误很抱歉,这起作用了,非常感谢!!!真的很有帮助!我删除了ru-ru,它成功了
public class Player
{
/* Some other properties */
[Required(ErrorMessage = "Укажите среднее количество блокшотов")]
[Range(0, 10.0, ErrorMessage = "Недопустимое значение, до 10")]
public float BlockPerGame { get; set; }
/* Some other properties */
}
@using (Html.BeginForm())
{
@Html.HiddenFor(m => m.Id)
<div class="box-form">
/* Some other properties */
<div class="text-style-roboto form-group">
<label>Среднее количество блокшотов</label>
@Html.TextBoxFor(m => m.BlockPerGame, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.BlockPerGame)
</div>
/* Some other properties */
<div class="form-group">
<button type="submit" class="button button-create" id="button-create">Добавить</button>
@Html.ActionLink("Отмена", "Index", null, new { @class = "button button-cancel", id = "button-cancel" })
</div>
</div>
}
public class AdminController : Controller
{
/*Some other methods*/
[HttpPost]
public async Task<ActionResult> Edit(Player player, string ChoosingTeam)
{
if (ModelState.IsValid)
{
if (ChoosingTeam != string.Empty)
{
try
{
player.TeamId = int.Parse(ChoosingTeam);
await repository.SavePlayerAsync(player);
TempData["message"] = string.Format("Игрок {0} {1} сохранены", player.Name, player.Surname);
return RedirectToAction("Index");
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
}
}
IEnumerable<SelectListItem> list = new SelectList(repository.Teams, "Id ", "Name");
ViewBag.ChoosingTeamName = list;
return View(player);
}
}
DefaultModelBinder.ResourceClassKey = "ErrorMessages";