Model验证功能
2018-06-23 22:33:08来源:未知 阅读 ()
占位符
[MetadataType(typeof(Login_UserMetaData))] public partial class Login_User { //验证需要用到 public class Login_UserMetaData { [Display(Name="用户名")] [Required(ErrorMessageResourceName="Required",ErrorMessageResourceType=typeof(Resource))] public string UserName { get; set; } [Display(Name="密码")] [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resource))] public string Password { get; set; } } }
然后控制器大概就需要这样的代码,记得action方法传进来的参数类型是类名,如果用string username这种形式,ModelState.IsValid将无效
[HttpPost] public ActionResult Login(Login_User loginUser) { if (!ModelState.IsValid) {//验证不通过返回 return View(); } Login_User user=userManager.Login(loginUser.UserName, loginUser.Password); if (user == null) {//用户密码错误! ViewBag.error = "用户名或密码错误!"; return View(); } Session["user"] = user; return RedirectToAction("index", "home"); }
@{ ViewBag.Title = "后台登陆"; } @model Letter.Data.Model.Login_User <script src="~/Scripts/jquery.validate.min.js"></script> <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> @using(Html.BeginForm()){ <div> <label for="username">用户名:</label> @Html.TextBoxFor(u=>u.UserName) @Html.ValidationMessage("UserName") </div> <div> <label for="password">密码:</label> @Html.PasswordFor(u=>u.Password) @Html.ValidationMessage("Password") </div> <div> <input type="submit" value="登录" />@ViewBag.error </div> }
web.config应该有如下配置才有效
附上一个比较完整的验证
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; namespace RegisterValidation.Models { public partial class User { [Key] public int ID { get; set; } [Display(Name="姓名")] [Required(ErrorMessageResourceName="Required",ErrorMessageResourceType=typeof(Resource))] public string Name { get; set; } [Display(Name="生日")] //[RegularExpression(@"^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$",ErrorMessageResourceName="Regular",ErrorMessageResourceType=typeof(Resource))] [Required(ErrorMessageResourceName="Required",ErrorMessageResourceType=typeof(Resource))] public DateTime? Birthday { get; set; } [Display(Name="年龄")] [Range(18,60,ErrorMessageResourceName="Range",ErrorMessageResourceType=typeof(Resource))] public int Age { get; set; } [Display(Name="用户名")] [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resource))] public string Username { get; set; } [Display(Name="密码")] [Required(ErrorMessageResourceName="Required",ErrorMessageResourceType=typeof(Resource))] public string Password { get; set; } [Display(Name="邮箱")] [Required(ErrorMessageResourceName="Required",ErrorMessageResourceType=typeof(Resource))] [RegularExpression(@"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$",ErrorMessageResourceName="Regular",ErrorMessageResourceType=typeof(Resource))] public string Email { get; set; } } public class UserDBContext : DbContext { public DbSet<User> Users { get; set; } } }
另一个部分user类,包含确认密码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; namespace RegisterValidation.Models { public partial class User { [Display(Name="确认密码")] [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resource))] [StringLength(10,MinimumLength=5,ErrorMessageResourceName="StringLength",ErrorMessageResourceType=typeof(Resource))] [Compare("Password",ErrorMessageResourceName="Compare",ErrorMessageResourceType=typeof(Resource))] public string PasswordConfirm { get; set; } } }
资源文件如下
效果如下
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:.NET程序如何启动?
- js的验证表单 2020-03-18
- 详解HTML5 使用video标签实现选择摄像头功能 2020-02-07
- vue路由跳转时判断用户是否登录功能的实现 2020-01-17
- js插件实现图片滑动验证码 2020-01-17
- jQuery EasyUI tree增加搜索功能的实现方法 2019-12-17
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash