0

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!

d_jeter54
  • 46
  • 1
  • 9

2 Answers2

0

You are not receiving a value for F_Alta from the View, because you neither have an input nor a hidden field for it. "01/01/0001 12:00:00 a.m." is just the default value for a DateTime object. DateTime can never be null.

You should use EditorFor, not DisplayFor in your View. DisplayFor is only for displaying the data. Also, Use data annotation to define what you want to be displayed in your label, instead of hard-coding the label in your View:

<div class="editor-label">
    @Html.LabelFor(model => model.F_Alta)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.F_Alta)
    @Html.ValidationMessageFor(model => model.F_Alta)
</div>

Model metadata class:

public class AlmacenMetaData
{

    [Required(ErrorMessage = MensajesValidacionAlmacen.DescripcionRequerido)]
    [StringLength(32, ErrorMessage = MensajesValidacionAlmacen.DescripcionLongitud)]
    public object D_Almacen
    {
        get;
        set;
    }

    [Display(Name = "Fecha de alta")]
    [DisplayFormat(DataFormatString = Formatos.Fecha, ApplyFormatInEditMode = true)]
    public object F_Alta
    {
        get;
        set;
    }
}
ataravati
  • 8,891
  • 9
  • 57
  • 89
0

Try this for displaying DateTime:

[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}")]
public DateTime Date 
{
    get
    {
        return DateTime.Now;
    }
    set
    {
        Date = DateTime.Now;
    }
}

Will produce the following in a view:

@Html.DisplayFor(x => x.Date)        // 2013/05/23
@Model.Date                          // 23/05/2013
@Html.DisplayFor(x => Model.Date)    // 2013/05/23

Obviously change format to suit your needs. See more here.

Community
  • 1
  • 1
MattSull
  • 5,514
  • 5
  • 46
  • 68
  • I tried to use your solution but unfortunately when Submit the form, the date value changed to 01/01/0001 12:00:00 a.m. – d_jeter54 Jul 29 '13 at 21:47