1

In model I am using below code:

[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(InformationResources))]
    [DataType(DataType.Date, ErrorMessageResourceName = "DateInvalid", ErrorMessageResourceType = typeof(InformationResources))]
    [DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
    [FutureDate(ErrorMessageResourceName = "DateInPast", ErrorMessageResourceType = typeof(InformationResources))]
    public DateTime? FirstTrade { get; set; }

In view I am using below code:

<div>
        @Html.TextBoxFor(m => m.FirstTrade, new { @Value = Convert.ToDateTime(Model.FirstTrade).ToString("yyyy/MM/dd"),@class = "form-control input-sm", @type = "date", @max = DateTime.Now.AddYears(1).ToString("yyyy-MM-dd") })
        @Html.ValidationMessageFor(model => model.FirstTrade)
    </div>

Now if I select data from date time picker in "yyyy/MM/dd" format then I am getting error message as "The field FirstTrade must be a date". One more thing that I am using en-Au locale. Please let me know how to remove the error message If i choose date in "yyyy/MM/dd" format.

Thanks in advance.

Raj
  • 141
  • 2
  • 10

1 Answers1

2

You should use the format you like when displaying the date value.

Try this:

@Html.TextBoxFor(m => m.FirstTrade, "{0:yyyy/MM/dd}", new { @Value = Convert.ToDateTime(Model.FirstTrade).ToString("yyyy/MM/dd"),@class = "form-control input-sm", @type = "date", @max = DateTime.Now.AddYears(1).ToString("yyyy-MM-dd") })

This is how my model and view look like:

Model:

public class Order
    {
        public int OrderId { get; set; }
        [Display(Name = "Incorporation date")]
        public DateTime? IncorporationDate { get; set; }

        [Display(Name = "Capital payment date")]
        public DateTime? CapitalPaymentDate { get; set; }
    }

View:

@Html.LabelFor(model => model.IncorporationDate)
        <div class="input-prepend">
            <span class="add-on"><i class="icon-briefcase"></i></span>
            @Html.TextBoxFor(model => model.IncorporationDate, "{0:dd.MM.yyyy}", new { @class = "span3 date", @data_date_format = "dd.mm.yyyy", @placeholder = "Incorporation date" })
        </div>

        @Html.LabelFor(model => model.CapitalPaymentDate)
        <div class="input-prepend">
            <span class="add-on"><i class="icon-briefcase"></i></span>
            @Html.TextBoxFor(model => model.CapitalPaymentDate, "{0:dd.MM.yyyy}", new { @class = "span3 date", @data_date_format = "dd.mm.yyyy", @placeholder = "Capital payment date" })
        </div>

Edit:

Try setting the globalization tag under system.web to the culture you need:

<system.web>
  <globalization uiCulture="nb-NO" culture="nb-NO"/>
</system.web>
Vsevolod Goloviznin
  • 12,074
  • 1
  • 49
  • 50
  • I have already tried with this, It is not working. Please let me know any another solution. – Raj Nov 14 '14 at 08:47
  • I've had the same problem and my solution worked. Have you tried using my solution and removing the DisplayFormat attribute from your model? And what version of MVC you're using? – Vsevolod Goloviznin Nov 14 '14 at 09:05
  • Thanks for your reply. I have removed the display formet from model but now after selecting the date from date time picker I am getting error message as " Please enter a value greater than or equal to 0.".Please let me know how to resolve this. – Raj Nov 14 '14 at 09:12
  • What datepicker are you using? – Vsevolod Goloviznin Nov 14 '14 at 09:25
  • I m using jquery datetimepicker – Raj Nov 14 '14 at 09:31
  • I am also doing the same way but getting error message "Please enter a value greater than or equal to 0." – Raj Nov 14 '14 at 09:34
  • @Raj Hm, that's really strange. The last resort solution is to make the field String and implement custom validator for it – Vsevolod Goloviznin Nov 14 '14 at 09:39
  • Thanks for your try. Atleast u tried to give the answer. If u have any other solution please let me know. – Raj Nov 14 '14 at 09:41
  • In my project I'm suing datepicker from bootstrap, may be you can try using it instead of jq datepicker – Vsevolod Goloviznin Nov 14 '14 at 09:43
  • @Raj I've forgotten about web.config change that I've made, try to add your culture in the globalization tag, I've updated the answer with my code – Vsevolod Goloviznin Nov 14 '14 at 09:47
  • can you please give me your facebook Id so that I can ask you from there. We cannt extend discussion here more. – Raj Nov 14 '14 at 09:59