-1

I searched almost every topic on this site regarding date format also DatePicker however couldn't find any solution for my issue, also applied This with no success

Problems

  1. Date format is not applied in Partial views
  2. datepicker is not applied in Partial views

.cs

    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime? RefDate{ get; set; }

Razor (Partial View)

In my main view, when Edit is clicked, i load a partial view inside a div, the partial view contains:

@Html.TextBoxFor(model => model.RefDate, new { @class = "form-control input-sm" })

DateTime.cshtml

@model DateTime?
@Html.TextBox("", (Model.HasValue ? Model.Value.ToString("dd/MM/yyyy") :string.Empty),
new { @class = "datepicker" ,@readonly = "readonly"})

Web.Config

<globalization culture='fr-BE' uiCulture='fr-BE' />

Result

02/18/2015 0:00:00

IF i use

@Html.EditorFor(model => model.RefDate, new { @class = "form-control input-sm" })

Result I don't see any value but this text:

mm/dd/yyyy

Also bootstsrap format is gone.

Community
  • 1
  • 1
Maro
  • 2,579
  • 9
  • 42
  • 79
  • `TextBoxFor()` will not use your `EditorTempate` (`DateTime.cshtml`). To use `TextBoxFor()` with a format, it needs to be `@Html.TextBoxFor(model => model.RefDate, "{0:dd-MM-yyyy", new { @class = "form-control input-sm" })` –  Apr 10 '16 at 00:45
  • `EditorFor()` will call your template (although its unclear what the point of that template is - its should be just `@Html.TextBox("", new { @class = "datepicker" ,@readonly = "readonly"})` but it sounds like you might be generating the browsers HTML5 datepicker (does the html have `type="date"`? –  Apr 10 '16 at 00:48

1 Answers1

0

It seems your DateTime.cshtml template file is not correctly located, it should be in a folder named "EditorTempaltes" in the "Shared" view folder, or in your current view folder, like that :

enter image description here

Or like that :

enter image description here

And use Html.EditorFor to call your template.

Edit :

Try to decorate your property with the UiHintAttribute to indicate that you model use the DateTime.cshtml template file :

[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
[UIHint("DateTime")]
public DateTime? RefDate{ get; set; }
Fabien ESCOFFIER
  • 4,751
  • 1
  • 20
  • 32
  • My DateTime.cshtml is correctly located. also it works with normal view but not partial view. – Maro Apr 10 '16 at 09:34