ASP.NET MVC 3远程验证设置自定义错误消息 [英] ASP.NET MVC 3 Remote Validation set custom error message

查看:197
本文介绍了ASP.NET MVC 3远程验证设置自定义错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在MVC 3.0远程验证逻辑根据设定不同的错误

Is there any way to set different errors according with logic in MVC 3.0 remote validation

public ActionResult IsUserEmailExists(string email)
        {
            bool isExists = service.IsUserExists(email);

            if(isExists )
             //Set error message
             return Json(!isExists, JsonRequestBehavior.AllowGet);
            else if(something)
              //another logic
              //Set errror message
              return Json(something, JsonRequestBehavior.AllowGet);             
        }

通过使用来自属性声明中只值的ErrorMessage默认的远程验证

By default Remote validation using only ErrorMessage value from attribute declaration

[Remote("IsUserEmailExists", "Account", ErrorMessage = "User with such email already exists")]

有没有什么办法改变这种行为?

is there any way to change that behavior?

推荐答案

您可以返回错误信息,而不是一个布尔值:

You could return the error message instead of a boolean value:

return Json("Some custom error message", JsonRequestBehavior.AllowGet);

在此情况下,模型将被视为无效(同样的,如果你已经返回),并作为错误信息的字符串。

In this case the model will be considered invalid (the same as if you had returned false) and the string used as error message.

这篇关于ASP.NET MVC 3远程验证设置自定义错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆