The problem is that I want to display a Date as readonly or disabled in "{0:dd/MM/yyyy}" format, following the Blog's solutions I have not got the expected results, because when I use a DisplayFor helper works for display the date in correct form but when I do the post the value of the date changes to {01/01/0001 12:00:00 a.m.}, and receive next error "The conversion of a datetime2 data type to a smalldatetime data type resulted in an out-of-range value. The statement has been terminated."
Code in my view:
<div class="editor-label">
@Html.LabelFor(model => model.F_Alta, "Fecha de alta:")
</div>
<div class="editor-field">
@Html.DisplayFor(model => model.F_Alta)
</div>
My class:
[MetadataType(typeof(AlmacenMetaData))]
public partial class Almacen
{
}
public class AlmacenMetaData
{
[Required(ErrorMessage = MensajesValidacionAlmacen.DescripcionRequerido)]
[StringLength(32, ErrorMessage = MensajesValidacionAlmacen.DescripcionLongitud)]
public object D_Almacen
{
get;
set;
}
[DisplayFormat(DataFormatString = Formatos.Fecha, ApplyFormatInEditMode = true)]
public object F_Alta
{
get;
set;
}
}
Code in my controller:
[HttpPost]
public ActionResult Editar(Almacen almacen)
{
if (ModelState.IsValid)
{
db.Almacen.Attach(almacen);
db.ObjectStateManager.ChangeObjectState(almacen, EntityState.Modified);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(almacen);
}
Any suggestion?, thanks!