I am working with razor view engine in asp.net mvc3.
Now, I need an input for a DateTime
which should display the value in a fixed format (say dd-MMM-yyyy
). So I can do:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MMM-yyyy}")]
public DateTime StartDate { get; set; }
And in the view:
@Html.EditorFor(model => model.StartDate)
But I need to add a class in the input. Which I think is not possible in EditorFor
.
So I could use
@Html.TextBoxFor(model => model.StartDate, new { @class = "Date" })
But the display format does not work in this case.
Model
can be null. So,
@Html.TextBox("StartDate", string.Format("{0:dd-MMM-yyyy}", Model.StartDate))
will throw NullReferenceException
.
@Html.TextBoxFor(model => model.StartDate, new { @class = "Date" })
Can your date return a null value I think the exception is because your date may be null – InvoluntaryApplyFormatInEditMode
. I miss that parameter. – Karrah