my model:
public class Unidad
{
public int UnidadID { get; set; }
[Required(ErrorMessage ="Unidad Abrevidad no puede ser vacio")]
[StringLength(50)]
[Display(Name ="Unidad Abrevidad")]
public string AbbreviatedUnit { get; set; }
[Required(ErrorMessage ="Descripcion no puede ser vacio")]
[StringLength(200,MinimumLength =3)]
[Display(Name = "Descripción")]
public string description { get; set; }
}
located in the controller my method delete
public ActionResult Delete(int id, Unidad unidad)
{
try
{
if (ModelState.IsValid)
{
Unidad deleteunidad = TempRepository.EditOrDelete(id);
if (deleteunidad == null)
{
return HttpNotFound();
}
TempRepository.DeleteUnidad(deleteunidad);
return RedirectToAction("Index");
}
// TODO: Add delete logic here
return View(unidad);
}
catch
{
return View(unidad);
}
}
located in the UnitRepository class my method to find unity
internal Unidad FindUnidad(int? id)
{
using (var db = new CNRContext())
{
var unidad = db.Unit.Find(id);
return unidad;
}
}
my method to erase the unit
internal void DeleteUnidad(Unidad unidad)
{
using (var db = new CNRContext())
{
db.Entry(unidad).State = EntityState.Deleted;
db.SaveChanges();
}
}
my ModelState.IsValid is always false when I try to delete a unit ??