ModelState.AddModelError使用

ModelState.AddModelError使用

大家好,又见面了,我是全栈君。

后台:ModelState.AddModelError("userPwd", "请输入密码!");
ModelState是一个字典类型,这句话的作用是向ModelState中添加一条错误信息,第一个参数是Key,第二个参数是Value。


前台:Html.ValidationMessage(key) 或 Html.ValidationSummary() 来显示


对于什么时候使用ModelState.AddModelError(模型错误)的思考与总结:
针对马拉松赛事:
(1)当前的赛事是否存在
 var eventMessage = _eventSystemMessageService.GetAllEventSystemMessages().FirstOrDefault(x => x.EventName == currentEventName);  //查询出该条数据
                if (eventMessage == null)//判断是否存在
                {
                    string no_NowEvent = _localizationService.GetResource("Event.EventRegister.No_NowEvent");   //进行语言转换
                    ModelState.AddModelError("", no_NowEvent);//"不存在当前赛事"
                    InitRegisterModel(model);
                    return View(model);
                }

(2)证件号码是否存在

 var rms = _registratorMessageService.GetAllRegistratorMessages();  //所有数据

                var entity = rms.FirstOrDefault(r => r.RegistratorDocumentNumber == model.RegistratorDocumentNumber);//通过输入的数据与数据库的数据匹配得出一条数据
                if (entity != null)
                {
                    string hadDocumentNumber = _localizationService.GetResource("Event.EventRegister.HadDocumentNumber");
                    ModelState.AddModelError("", hadDocumentNumber);//"证件号码已存在"
                    InitRegisterModel(model);
                    return View(model);
                }

 

(3)手机号码是否存在

                var phoneValidEnity = rms.FirstOrDefault(r => r.RegistratorPhone == model.RegistratorPhone);//针对手机号查询数据
                if (phoneValidEnity != null)
                {
                    string hadPhoneNumber = _localizationService.GetResource("Event.EventRegister.PhoneNumber");
                    ModelState.AddModelError("", hadPhoneNumber);//"手机号码已存在"
                    InitRegisterModel(model);
                    return View(model);
                }

(4)邮箱地址(号码)是否存在

 var emailEntity = rms.FirstOrDefault(x => x.RegistratorEmail == model.RegistratorEmail);//通过邮箱查询匹配数据
                if (emailEntity != null)
                {
                    string yourEmailIsUsed = _localizationService.GetResource("Event.EventRegister.YourEmailIsUsed");
                    ModelState.AddModelError("", yourEmailIsUsed);//"该邮箱地址已被使用,请使用其他邮箱地址"
                    InitRegisterModel(model);
                    return View(model);
                }

 

(5)生日不准确

  try
                {
                    Enmodel.RegistratorBirth = new DateTime(model.DateOfBirthYear, model.DateOfBirthMonth, model.DateOfBirthDay);
                }
                catch
                {
                   
                    string birthDayWrong = _localizationService.GetResource("Event.EventRegister.birthDayWrong");
                    ModelState.AddModelError("", birthDayWrong);// "生日输入不正确"
                    return

…….

总的来说:对于使用模型错误是根据需求来定的,主要起验证作用。

使用场景:

(1)注册时

(2)找回密码时

posted on
2017-03-10 16:30
WFaceBoss 阅读(
) 评论(
)
编辑
收藏

转载于:https://www.cnblogs.com/wfaceboss/p/6531355.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108629.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • centos7配置sftp_怎么修改服务器sftp端口

    centos7配置sftp_怎么修改服务器sftp端口CentOS7修改sftp端口修改sftp默认端口为12322vi/etc/ssh/ssh_config把Port22改成Port12322vi/etc/ssh/sshd_config把Port22改成Port12322修改之后重启servicesshdrestart

    2025年11月17日
    3
  • BufferedWriter[通俗易懂]

    BufferedWriter[通俗易懂]  带有默认缓冲的字符输出流。使用:  构造:BufferedWriterbf=newBufferedWriter(“outFilePath”);  写入:write(“thing”);  换行:newLine();  刷新缓冲区,会将内容写进目标文件:flush();  关闭该流的操作:close();importjava.io.BufferedWrit…

    2022年5月1日
    47
  • 做事的真正态度

    做事的真正态度

    2022年1月12日
    50
  • 2022 CCF中国开源大会会议通知(第四轮)[通俗易懂]

    2022 CCF中国开源大会会议通知(第四轮)[通俗易懂]2022CCF中国开源大会(CCFChinaOSC)拟于2022年8月20日至21日在陕西省西安高新国际会议中心召开。大会由中国计算机学会(CCF)与开放原子开源基金会主办,CCF开源发展委员会、西安电子科技大学承办,西安电子科技大学计算机科学与技术学院(国家示范性软件学院)具体落实,西安市智能软件工程重点实验室负责实施,得到西安高新区管委会的大力支持,西安软件园发展中心具体执行。大会由华为技术有限公司作为白金赞助商,阿里巴巴集团、汇丰科技、东软集团股份有限公司、江苏引跑网络科技有限公司作为赞助商。本届

    2025年11月21日
    4
  • 点到圆的最近距离公式推导

    点到圆的最近距离公式推导该距离公式在 CircleFittin 相关的一篇文章中用到 现实现其推导过程 设圆的一般的方程形式 任一点 P 的坐标 点 P 到圆上点得最短距离的公式 推导过程 1 由圆一般方程形式可以推导出圆的标准方程形式 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 且有 即 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 圆心坐标 nbsp 半径 2 点到圆的最短距离 nbsp nbsp nbsp nbsp nbsp nbsp 点到圆的最短距离等于点到圆心的距离减去半径的绝

    2025年6月10日
    3
  • 初次使用cocoapods注意事项「建议收藏」

    初次使用cocoapods注意事项

    2022年2月1日
    43

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号