Input box as “date” on web when model is “int”

Use View Model to bind view instead of actual model, which will contain DateTime type of property. Then Map View Model to actual Entity Model.

View Model:

class AgrRowVM
{
  public DateTime? FrDt {get; set;}
  //Rest of the properties
}

Model:

class AgrRow
{
  public int? FrDt {get; set;}
  //Rest of the properties
} 

After posting values to serverside map View Model to Orginal model.

if(agrRowVMObj.FrDt.HasValue)
{
  agrRowObj.FrDt = int.Parse(agrRowVMObj.FrDt.ToString("yyyyMMdd"));
}

Leave a Comment