mvc model数据验证

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yanglianchun0116/article/details/73549489

 创建UserInfo类

   public class UserInfo

    {

        [Required(ErrorMessage="用户名必填")]

        [MinLength(6, ErrorMessage = "用户名至少6位")]

        public string username { set; get; }

         [Required(ErrorMessage = "密码必填")]

        public string password { set; get; }

        [Compare("password", ErrorMessage = "两次密码输出不相同")]

        public string repassword { set; get; }

        public string age { set; get; }

    }


    /************  控制器    ************/

  public ActionResult Add(UserInfo ui)
        {
           
            if (ModelState.IsValid) //模型验证全部通过
            {
               //添加代码
            }

            return View("~/views/home/index.cshtml");

     }


      /************   视图    ************/


       

展开阅读全文

asp.net mvc数据验证的二个问题

08-25

1.在注册页面,因为要牵涉到很多自定义验证,所以打算使用jquery.validate.js,但是发现就算使用这个还是无法屏蔽掉mvc本身的验证,依然会产生诸如[code=JScript]if (!window.mvcClientValidationMetadata) window.mvcClientValidationMetadata = []; rnwindow.mvcClientValidationMetadata.push("Fields":["FieldName":"UserName","ReplaceValidationMessageContents":false,"ValidationMessageId":"UserName_validationMessage","ValidationRules":["ErrorMessage":"请填写您的用户名","ValidationParameters":,"ValidationType":"required"],"FieldName":"Email","ReplaceValidationMessageContents":false,"ValidationMessageId":"Email_validationMessage","ValidationRules":["ErrorMessage":"请填写电子邮件","ValidationParameters":,"ValidationType":"required"],"FieldName":"Password","ReplaceValidationMessageContents":true,"ValidationMessageId":"Password_validationMessage","ValidationRules":["ErrorMessage":"请设置您的密码","ValidationParameters":,"ValidationType":"required"],"FieldName":"Mobile","ReplaceValidationMessageContents":true,"ValidationMessageId":"Mobile_validationMessage","ValidationRules":["ErrorMessage":"请填写手机号码","ValidationParameters":,"ValidationType":"required"],"FieldName":"ConfirmPassword","ReplaceValidationMessageContents":true,"ValidationMessageId":"ConfirmPassword_validationMessage","ValidationRules":["ErrorMessage":"请确认您密码","ValidationParameters":,"ValidationType":"required"]],"FormId":"form0","ReplaceValidationSummary":false);rn//]]>rnrn[/code]rnrn想屏蔽掉这些代码,但是好象没有想到较好的办法,如果去掉Html.EnableClientValidation(),好象同时不能使用jquery验证rnrnrn2.有个日期时间型字段,字段是非空型,但有默认值,所以在用户填写的时候也想弄成选填类型,但是不管怎么样,服务器端都会自动验证是否填写,即使去掉对应的Required都不管用,这个问题,当然可以增加一个隐藏文本框来通过js转化,但是觉得毕竟不是好的方法。rnrnrn上网查了一直没有找到较好的解决方案,不知大家有什么办法呢?或许是mvc本身的bug? 论坛

没有更多推荐了,返回首页