1

The title might not describe my problem very accurately, but I couldn't find a better way to describe it shortly.

Here's my problem; I'm creating an MVC application, and using the JQuery datepicker to make the pick a date from an input field. Whenever the action loads (/Report/Index), the first time, I need the date to be set to whatever is in the model. It does load the date, but the date format is incorrect: dd-mm-yyyy hh:mm:ss, though I'm changing the dateformat in the initialization function of the datepicker, just as seen here:

$('.datefield').datepicker({ dateformat: 'dd/mm/yy'});

Whenever i change the date within this input field, the format is correct.

How can I make the date from the model, to be the correct date format whenever the action loads?

Markup

<div>
    @Html.TextBoxFor(model => model.StartDate, new { @class = "datefield" })
</div>

Model snippet (StartDate)

public class ReportViewModel{

    public DateTime StartDate { get; set; }
}

If you need to see more code than this, you're more than welcome to ask

6
  • 1
    Try @Html.TextBoxFor(m => m.StartDate, "{0:dd/MM/yyyy}", new { @class = "datefield" }) Commented Feb 23, 2016 at 9:37
  • @StephenMuecke already tried that. It won't let me, and gives me an error. I guess I can't use methods like that within the helper. Commented Feb 23, 2016 at 9:45
  • Of course you can. And what error are your getting? (the code in my last comment works fine) Commented Feb 23, 2016 at 9:48
  • @StephenMuecke Oh wait sorry, I read your comment wrong (thought you meant ToString("dd/mm/yyyy"). Using your method gives the following output "23-02-2016". Is there a way for me to change the dash to a forward slash? Commented Feb 23, 2016 at 9:56
  • "{0:dd/MM/yyyy}" does not out put 23-02-2016 - it outputs 23/02/2016 - check you code again for errors/typos Commented Feb 23, 2016 at 9:59

1 Answer 1

2

Change the TextBoxFor() method to use the overload that includes a format string

@Html.TextBoxFor(m => m.StartDate, "{0:dd/MM/yyyy}", new { @class = "datefield" })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.