I am trying to disable validation of some field including password while inserting data. But I am getting the following error while inserting:
Cannot insert the value NULL into column 'Password', table 'Uni.Models.MyDb.dbo.Students'; column does not allow nulls. INSERT fails. The statement has been terminated.
My model class:
public class Student
{
[Key]
public int StudentId { get; set; }
[Required(ErrorMessage = "Name Required")]
public string Name { get; set; }
public string IdNumber { get; set; }
[Required(ErrorMessage = "Please Enter Password"), MinLength(5, ErrorMessage = "Password length error")]
public string Password { get; set; }
[Required, Compare("Password", ErrorMessage = "Invalid Confirm Password")]
public string ConfirmPassword { get; set; }
[Required(ErrorMessage = "Date Of Birth Required")]
[Column(TypeName = "Date")]
public DateTime BirthDate { get; set; }
public virtual Gender Gender { get; set; }
[Required(ErrorMessage = "Select Gender!")]
public int GenderId { get; set; }
}
My controller:
[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
public ActionResult add(Student std)
{
db.Configuration.ValidateOnSaveEnabled = false;
if (ModelState.IsValidField("Name") && ModelState.IsValidField("GenderId") && ModelState.IsValidField("BirthDate"))
{
Common common = new Common();
std.IdNumber = common.generateUsername("S-");
db.Students.Add(std);
db.SaveChanges();
db.Configuration.ValidateOnSaveEnabled = true;
return RedirectToAction("Index");
}
ViewBag.Genders = new SelectList(db.Genders, "GenderId", "Dari");
ViewBag.Provinces = new SelectList(db.Provinces, "ProvinceId", "Name");
return View();
}
I really appreciate anyone could help me with this and tell me where am I going wrong.