0

when i want to change my datetime format i take error.I use this code on view:

@Html.TextBoxFor(model => model.enddate, new { @Value =((DateTime)Model.enddate).ToString(" dd/MM/yyyy"),@class = "input-xlarge datepicker"})

but when i execute Model.enddate is null so i take this error:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Can you help me.

My english is not very well :(

Veysel Solak
  • 23
  • 1
  • 4

3 Answers3

1

If the model.enddate is legitimately null you could set the datatype to nullable in the model - DateTime? enddate

And you could add the DisplayFormat Attribute to the property

[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
 public DateTime? enddate{ get; set; }
MarkP
  • 4,745
  • 3
  • 22
  • 18
0

Check that from you controller you send an instance of the model with data, it seems that property enddate is null.

U. Busto
  • 194
  • 1
  • 17
0

You date seems to be nullable, format it only when a value is provided :

@Html.TextBoxFor(model => model.enddate, new { @Value =(Model.enddate.HasValue ? Model.enddate.Value.ToString(" dd/MM/yyyy") : ""),@class = "input-xlarge datepicker"})
Guillaume
  • 12,824
  • 3
  • 40
  • 48