4

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>
 }
Community
  • 1
  • 1
KiRa
  • 924
  • 3
  • 17
  • 42
  • You can also use ToShortDateString() at the end like @Html.EditorFor(model => model.VoluntaryWork.DateEnded.ToShortDateString() ) – Abhay Dixit Mar 22 '17 at 07:11
  • can you show your model class? – Abhay Dixit Mar 22 '17 at 07:33
  • I would strongly advise that you need to sort out the datetime issue inside your controller (or service class) either as part of the initial HttpGet action to populate the view, or inside the HttpPost when you send the values back. Either way, the remedial action must be taken at controller level. – Abhay Dixit Mar 22 '17 at 07:42

5 Answers5

4

You can apply a DisplayFormatAttribute to your property

[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime DateEnded { get; set; }

and the format will be respected by the EditorFor() method.

Alternatively you can use

@Html.TextBoxFor(m => m.VoluntaryWork.DateEnded, "{0:d}", null)
  • i try it just now `@Html.TextBoxFor(m => m.VoluntaryWork.DateEnded, "{0:d}")` but it still return the `. . AM` – KiRa Mar 22 '17 at 07:10
  • @StephenMuecke looks like you don't need 3rd parameter. `TextBoxFor` has this overload: `public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper, Expression> expression, string format)` – teo van kot Mar 22 '17 at 07:15
  • @teovankot, Yes, your correct - there is an overload for that (not sure why OP is claiming it did not work) –  Mar 22 '17 at 07:19
2

The simpliest way is:

@Html.TextBoxFor(model => model.VoluntaryWork.DateEnded, "{0:dd/MM/yyyy}")

Fortunatly EditorFor hepler doesn't have this overload. that's why you should use TextBoxFor html helper

Update:

MVC 3 doesn't have this overload. So the simpliest way to solve your problem will be use not strongly typed helper like this:

@Html.TextBox("VoluntaryWork_DateEnded", 
    Model.VoluntaryWork.DateEnded.HasValue
    ? Model.VoluntaryWork.DateEnded.Value.ToString("dd/MM/yyyy")
    : DateTime.Now.ToString("dd/MM/yyyy"))
teo van kot
  • 12,350
  • 10
  • 38
  • 70
0

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.

Abhay Dixit
  • 998
  • 8
  • 28
-1

Try this code

@Html.EditorFor(model => model.VoluntaryWork.DateEnded, 
    new { @Value = model.VoluntaryWork.DateEnded.ToString("MM/dd/yyyy") })
muratoner
  • 2,316
  • 3
  • 20
  • 32
  • 1
    Never set the `value` attribute when using the `HtmlHelper` methods! –  Mar 22 '17 at 07:09
  • You can set value attribute when using HtmlHelper because I can use my projects. – muratoner Mar 22 '17 at 07:11
  • Then your doing the wrong thing and it means you will never get true 2-way model binding. The method correctly sets the value based on (in order) `ModelState` values, `ViewData` values and finally the model value. –  Mar 22 '17 at 07:16
-1

To add a property to your model add this code:

public string ReturnDateForDisplay  
{  
    get  
    {  
       return this.VoluntaryWork.DateEnded.ToString("d");  
    }  
}

Then in your PartialView:

@Html.EditorFor(model => model.ReturnDateForDisplay)

Abhay Dixit
  • 998
  • 8
  • 28