建立一个登录Model
public class LoginModel { public string name; public string password; public string login() { using(var db=new DataEntities()) { var passMd5 = password.md5(); var user=db.Info.FirstOrDefault(u => u.userName == name); if(user==null) { return "User name not exist"; } if(user.passWord!=passMd5) { return "Password is invalid"; } else { return null; } } } }
在View中建立表单
在 Controller的HttpPost method里捕捉用户登录事件,但是Model中的值均为空
public ActionResult Login(LoginModel model) { var result = model.login(); if (result == null) { return RedirectToAction("Home", "User"); } else { ModelState.AddModelError("",result); return View(model); } }
原因:
Model里的字段必须是Property
因此,应该是
public string name{get;set;}