5

Please, consider the following example:

  1. view model

    public class FooViewModel
    {
        public DateTime Date { get; set; }
    }
    
  2. controller

    public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index(FooViewModel foo)
        {
            return View(foo);
        }
        [HttpPost]
        public ActionResult Submit(FooViewModel foo)
        {
            return RedirectToAction("Index", foo);
        }
    }
    
  3. view

    @model MvcApplication1.Models.FooViewModel
    <h1>@Model.Date</h1>
    @using (Html.BeginForm("Submit", "Home", FormMethod.Post))
    {
        @Html.TextBoxFor(m => m.Date)
        <input type="submit" value"Submit" />
    }
    
  4. routes

    routes.MapRoute(
        null,
        "",
        new { controller = "Home", action = "Index" }
    );
    routes.MapRoute(
        null,
        "{action}",
        new { controller = "Home" },
        new { action = "Submit" }
    );
    

The problem is that after form submit the date editor gets values with day and month switched:

  1. after first submit
  2. after resubmit

What can I do to make it work properly?


Actually I tried to format the value of the editor:

  1. @Html.TextBoxFor(m => m.Date, new { value = Model.Date.ToString("dd.MM.yyyy") })
  2. using editor templates, as shown in MVC 2 Editor Template with DateTime, only of course adopted to MVC3; and I also used EditorFor helper instead to make the template be utilized.
  3. using DataAnnotations: [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MMM/dd/yyyy}")]

None of these changed anything. Neither the value of the editor changed, nor it appeared formatted.


In the end I (followed the answer by DrJokepu) managed to make this work only with raw HTML, i.e. no helper methods for DateTime:

public static MvcHtmlString GetDateEditor(this FooViewModel model)
{
    string temp = "<input id=\"Date\" type=\"text\" value=\"{0}\" name=\"Date\" 
                    data-val-required=\"Please, enter a valid date\" 
                    data-val=\"true\" />";
    return MvcHtmlString.Create(
                 string.Format(temp, 
                               model == null 
                                    ? string.Empty 
                                    : model.Date.ToString("dd.MM.yyyy")));
    }

If there is a way to achieve the same result with any of the helper methods remains a secret to me...

Community
  • 1
  • 1
horgh
  • 17,918
  • 22
  • 68
  • 123
  • what if you format the date? – Hanlet Escaño Apr 11 '13 at 06:26
  • @HanletEscaño I do actually need to format the date in the editor, and this indeed should also solve the problem in question. However, I failed to format it anyway...I'll describe my attempts in the question in a while – horgh Apr 11 '13 at 06:31
  • i guess the date format in the database is different that is why its causing the problem you can apply the date format to your ViewModel (if you are using them to bind to the views?) – Dakait Apr 11 '13 at 06:38
  • 1
    @dakait Sorry, what database are you talking about? This example has no database at all. – horgh Apr 11 '13 at 06:40
  • @KonstantinVasilcov, can't you define the format in your model , something like in the question http://stackoverflow.com/questions/1961114/date-only-from-textboxfor – Habib Apr 11 '13 at 06:42
  • @Habib I've just checked it. With `TextBoxFor` it still does not apply the format given in the model – horgh Apr 11 '13 at 06:44
  • @KonstantinVasilcov ah my bad, i thought you are persisting the values in db after form submit... – Dakait Apr 11 '13 at 06:46
  • @Habib actually, it seems, I tried all the answers given in the question you linked and none of them seem to work. I must be doing something incorrectly....But what?? – horgh Apr 11 '13 at 06:48
  • @KonstantinVasilcov how about setting the date format in the application culture, here is link which shows how to do it http://stackoverflow.com/a/300853/1679410 – Dakait Apr 11 '13 at 07:13
  • @dakait thanks, but actually the culture settings reflect the format I need without any changes. However helper methods do not use it neither as is nor after I used the code in the link you gave. – horgh Apr 11 '13 at 07:21

1 Answers1

3

Please, try slightly changed version:
@Html.TextBoxFor(m => m.Date, new { @Value = Model.Date.ToString("dd.MM.yyyy") })

Vladimir
  • 7,345
  • 4
  • 34
  • 39
  • I was absolutely sure I tried this with capital letter `V` and `@` at the beginning and it did not work. I was wrong... – horgh Apr 11 '13 at 07:57