I have something code like this
@Html.EditorFor(model => model.VoluntaryWork.DateEnded)
@Html.ValidationMessageFor(model => model.VoluntaryWork.DateEnded)
and working fine. But It is retrieving the whole data from my sql
Output from current code
3/22/2017 12:00:00 AM
Desired output
3/22/2017
I try to use a code like this @Html.ValidationMessageFor(model => model.VoluntaryWork.DateEnded.Value.ToShortDateString())
but it gives me an error
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions
I try to search on google and found this but for me its a long method? And I am new in that method so I don't know how to use it.
Is there any shortest way to achieve the desired output?.
Update
Controller code
PersonVoluntaryWork pvw = db.PersonVoluntaryWorks.Single(vw => vw.VoluntaryWorksId == id);
return PartialView("_NewPersonVoluntaryWorks", pvw);
View
@model System.Models.PersonVoluntaryWork
@using (Html.BeginForm())
{
<td>
@Html.EditorFor(model => model.VoluntaryWork.DateEnded)
@Html.ValidationMessageFor(model => model.VoluntaryWork.DateEnded)
</td>
}
The simpliest way is:
Fortunatly
EditorFor
hepler doesn't have this overload. that's why you should useTextBoxFor
html helperUpdate:
MVC 3 doesn't have this overload. So the simpliest way to solve your problem will be use not strongly typed helper like this:
Try this code
You can apply a
DisplayFormatAttribute
to your propertyand the format will be respected by the
EditorFor()
method.Alternatively you can use
To add a property to your model add this code:
Then in your PartialView:
@Html.EditorFor(model => model.ReturnDateForDisplay)
Annotate your property in the model like this:
[DataType(DataType.Date)]
public DateTime? DateEnded{ get; set; }
It will hide the time from your dateEnded property.