0

I have a variable in my model of type Datetime, I am trying to display it in the View with the html helper. I did this

 @Html.TextBoxFor(model => model.dated, new {required = "required", type="date" }

but the input does not take any value

asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84
AnotherGeek
  • 874
  • 1
  • 6
  • 24
  • Use one of the DateTime.ToString() overloads (e.g. DateTime.ToString(String, IFormatProvider)). Or possibly even more appropriate - format the date to a localized string already in the controller, and pass that as to the view (through the view model). – Oskar Lindberg Oct 08 '14 at 11:25
  • Try this answer http://stackoverflow.com/a/7026781/492258 – asdf_enel_hak Oct 08 '14 at 11:27
  • In the source code I see that the value of the input is set like this value="09/12/2025 23:00:00", I know that it should be like this value="2014-10-10". I don't know how to fix i, I added value="2014-10-10" in the helper but that didn't help – AnotherGeek Oct 08 '14 at 11:29
  • Look to MSDN for this kind of information: http://msdn.microsoft.com/en-us/library/System.DateTime.ToString%28v=vs.110%29.aspx http://msdn.microsoft.com/en-us/library/system.iformatprovider%28v=vs.110%29.aspx http://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx – Oskar Lindberg Oct 08 '14 at 11:36
  • Actually, if you really want this to be part of your view (and not controller logic) you should probably go for InputExtensions.TextBoxFor(HtmlHelper, Expression>, String), as in http://msdn.microsoft.com/en-us/library/hh833694%28v=vs.118%29.aspx – Oskar Lindberg Oct 08 '14 at 11:40
  • Problem is whith helper it set itself the value of the input with the value in the controller which is not accepted by the input type date. The solution that I did is not to use the helper (probably it is not the best solution) and i set the value with tostring("yyyy-MM-dd") – AnotherGeek Oct 08 '14 at 11:47

1 Answers1

0

Here is a simple working example:

@model SimpleTest.Controllers.SimpleViewModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
    <head>
        <title>Simple Test</title>
    </head>
    <body>
        <div>
            @using (Html.BeginForm()) {
                // Use whatever format string that suits your purposes:
                @Html.TextBoxFor(model => model.Dated, "{0:d MMM yyyy}")

                <input type="submit" value="Submit value"/>
            }
        </div>
    </body>
</html>

And now the controller code (just for the sake of verifying the solution):

using System;
using System.Web.Mvc;

namespace SimpleTest.Controllers {

    public class DateController : Controller {

        public ActionResult Index(SimpleViewModel model) {

            // First time you visit the page, there is no view data
            // (model.Dated will be equal to DateTime.MinValue) so we set
            // the date to "now".
            //
            // When you submit data through the form in the view and henceforth,
            // the value of model.Dated will be resolved from view data,
            // and we simply pass it back to the view again so that the result is
            // visualized.

            var date = model.Dated == DateTime.MinValue
                ? DateTime.Now
                : model.Dated;

            return View(new SimpleViewModel {
                Dated = date
            });
        }

    }

    public class SimpleViewModel {

        public DateTime Dated { get; set; }

    }

}

If you try editing the date value in the text box, you will find that it is properly resolved by the default model binder and passed to the action in the controller.

Oskar Lindberg
  • 2,255
  • 2
  • 16
  • 36
  • I didn't know that the second parameter of html.textboxfor can be used to set the format. I resolved the problem by not using the helper but your solution is better. Thank you – AnotherGeek Oct 08 '14 at 13:38