【发布时间】:2018-12-30 13:04:10
【问题描述】:
如何更改“The value 'some value' is not valid for 'some property'”验证错误的语言?
有人可以帮忙吗?我想把图片中的错误翻译成俄语Error。我阅读了很多网站,尝试使用RegularExpression,但它没有帮助
可能是我不正确理解如何做到这一点?
我只需要翻译错误,不需要改变文化。
在 web.config 中:
<globalization culture="en" uiCulture="en" />
我的具有数据注释属性的实体:
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);
}
}
【问题讨论】:
-
您输入的浮点数无效(您使用 ',' 而不是 '.' 作为分隔符)。如果您使用 [RegularExpression] 而不是浮点数作为数据类型的字符串怎么办?通过这种方式,您可以添加自定义错误消息
标签: c# asp.net-mvc validation localization model-binding