我想在服务器端验证某些表单字段,但是我不想使用数据注释自定义验证器。
我需要根据返回的“业务层方法”的定义来手动设置其值,以定义此消息。
举个例子!
需要:
如果给定的用户名已经存在,则MVC4验证错误范围应显示“此用户名已存在”。
代码:
if (_business.UserNameExists(username))
{
// Set the field validation error span message
// HOW TO DO??
}
最佳答案
一位 friend 提供了解决方案,这很简单!
if (_business.UserNameExists(username))
{
// Set the field validation error span message
ModelState.AddModelError("UserName", "This username already exists.");
}
其中UserName是要验证的Entity属性的名称。
关于asp.net-mvc - MVC4 : Manually set the validation message from the server side,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11553159/