[HttpPost]
public ActionResult Create(Users user)
{
if (ModelState.IsValid)
{
db.Users.Add(user);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
ModelState.IsValid
总是假的
所以它只是返回视图并且没有添加新记录..
修改
用户:
public class User
{
public int UserID { get; set; }
public string Name { get; set; }
[Display(Name = "Confirm Password")] [DataType(DataType.Password)]
public string ConfirmPassword { get; set; }
public string Designation { get; set; }
[Display(Name = "Date of Join")] [DataType(DataType.Date)] public DateTime DOJ { get; set; }
public string Email { get; set; }
[Display(Name = "Phone Number")] public System.Int64 PhoneNo { get; set; }
}
答案 0 :(得分:82)
ModelState.IsValid
将为false。
检查ModelState.Errors
导致此问题的原因。你可以用这个:
var errors = ModelState.Values.SelectMany(v => v.Errors);