I am trying to validate my model data which is a text box for string entry but when I try to test with int entry, I get no error and it is saved to the database ...
How can I prevent this?
Model class:
[Required]
public string DeptName { get; set; }
Action method:
public ActionResult Update(Dept model)
{
if (!ModelState.IsValid)
{
return View();
}
db.Entry(model).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
View:
@model Dept
@using (Html.BeginForm())
{
@Html.HiddenFor(a => a.DeptId);
@Html.TextBoxFor(a => a.DeptName);
@Html.ValidationMessageFor(a => a.DeptName);
<input value="Update" type="submit" class="btn btn-info" />
}