gpt4 book ai didi

asp.net-mvc - MVC4 : Manually set the validation message from the server side

转载 作者:行者123 更新时间:2023-12-04 13:28:22 24 4
gpt4 key购买 nike

我想在服务器端验证某些表单字段,但是我不想使用数据注释自定义验证器。
我需要根据返回的“业务层方法”的定义来手动设置其值,以定义此消息。

举个例子!

需要:

如果给定的用户名已经存在,则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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com