占位符
[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; } }}
资源文件如下
效果如下
新闻热点
疑难解答