本人小白想用asp.net MVC5写一个登陆,结果ModelState.IsValid一直为false 登陆不上去,
代码是淘来的,不会改,跪求大神指导。
LoginController:
public class LoginController : Controller { // GET: Login public ActionResult Index() { return View(); } public static Models.doctors LoginDemo(Models.doctors model) { //初始化EF上下文容器 BaseContext db = new BaseContext(); return db.doctors.FirstOrDefault(m => m.user_name == model.user_name && m.Password == model.Password); } [HttpPost] public ActionResult Index(Models.doctors model) { try { if (ModelState.IsValid) { var userinfo = LoginDemo(model); if (userinfo != null) { return RedirectToAction("Index", "Home"); } } } catch (Exception ex) { ModelState.AddModelError("", ex.Message); } return View("Error"); } }
View:
@using (Html.BeginForm()) { @Html.ValidationSummary(true) <div class="form-group" style="height:40px;margin-top:50px"> <div class="row"> @Html.LabelFor(m => m.user_name, new { @class = "col-md-5 control-label", @style = "text-align:right", @for = "account" }) <div class=" col-md-7"> @Html.TextBoxFor(m => m.user_name, new { @class = "form-control", placeholder = "请输入用户名", @style = "width:240px;", id = "account" }) @Html.ValidationMessageFor(m => m.user_name) </div> </div> </div> <div class="form-group" style="height:40px;"> <div class="row"> @Html.LabelFor(m => m.Password, new { @class = "col-md-5 control-label", @style = "text-align:right", @for = "pwd" }) <div class=" col-md-7"> @Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = "请输入密码", @style = "width:240px;", id = "pwd" }) @Html.ValidationMessageFor(m => m.Password) </div> </div> </div> <div class="form-group"> <div class="col-md-5"></div> <div class="col-md-2 col-xs-4"> <input type="submit" value="登录" class="btn btn-default" /> </div> <div class="col-md-5 col-xs-8"> <input type="reset" value="取消" class="btn btn-default" /> </div> </div> }
Models:
public class doctors { public int ID { get; set; } [Display(Name = "医生姓名")] [Required(ErrorMessage = "必填")] [StringLength(10, MinimumLength = 1, ErrorMessage = "必须是[1,10]个字符")] public string doctor_name { get; set; } [Display(Name = "账号")] [Required(ErrorMessage = "必填")] [StringLength(10, MinimumLength = 6, ErrorMessage = "必须是[6,10]个字符")] public string user_name { get; set; } [Display(Name = "密码")] [Required(ErrorMessage = "必填")] [StringLength(10, MinimumLength = 6, ErrorMessage = "必须是[6,10]个字符")] public string Password { get; set; } public virtual ICollection<registers> registers { get; set; } }
数据文上下类:
namespace shujukulianxi.DAL { public class BaseContext : DbContext { /// <summary> /// 构造函数中的 base("AccountContext") 。 /// 默认情况下和类名一样,即AccountContext,我们显式的给他指定出来。 /// </summary> public BaseContext():base("BaseContext") { } public DbSet<MrItems> MrItems { get; set; } public DbSet <patients> patients { get; set; } public DbSet<registers> registers { get; set; } public DbSet<doctors> doctors { get; set; } } }
你的 doctor_name 是必填, 但是你的 form 里并没有这个字段呀
或者你把 doctor_name 删掉吧, 感觉这个字段在你的demo里并没有用处
谢谢大神
看传入参数的值.然后看错误的详细内容.能看到哪个字段不符合哪个校验
谢谢大神