While working on an MVC3 project I need to Show the short date & time in the display for HTML helper, but when I used it is throwing the above error, the reason behind it was that the HTML helper DisplayFor and EditorFor takes directly the properties of the model rather than the functions which are associated with it.
@Html.DisplayFor(model => model.DateStart.Value.ToShortDateString( ))
To fix the above situation you can simply remove the DisplayFor and use as :
@if(Model.DateStart!=null)
{
Model.DateStart.ToShortDate( )
}
[DisplayName("Start Date")]
[DisplayFormat(DataFormatString="{0:MM/dd/yyyy}", ApplyFormatInEditMode=true)]
public System.Nullable<System.DateTime> ModifiedStartDate
{
get
{
return this.StartDate.ToShortDateString( );
}
set
{
this.StartDate = value;
}
}
How to use it:
@Html.DisplayFor(model => model.ModifiedStartDate)
I loved the solution
ReplyDelete