1

A have a problem which is I am sending Datetime to view. In HTTPGET i havent any problem i mean it shows date as formatted. But when i try to post it to controller it does not post. The date changing. For example if my date 25-03-2015 (25th March 2015) on posting its changing, and trying to take "25" as mounth. When i enter 11/10/2017 everything is ok. When i enter first digit (which is 11 in this example) higher than 12 it does not post. You can see my model and view parts. Thank you.

Model:

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

View:

<div class="input-capsule appointment-date">
     @Html.LabelFor(m => m.AppointmentDate, "Randevu Tarihi:")
     @Html.TextBoxFor(m => m.AppointmentDate, "{0:dd-MM-yyyy HH:mm}", new {@class = "form-control required" })
</div>
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Omera
  • 71
  • 5
  • 1
    Do you also have client side validation (using `jquery.validate.js` and `jquery.validate.unobtrusive.js`)? If so you need to reconfigure the validator (which validates dates based on `MM/dd/yyyy` format) –  Mar 25 '17 at 22:12
  • Thank you Stephen i posted the link where i solved it. – Omera Mar 27 '17 at 10:20
  • Link only answers are not acceptable, and since that solved your problem (even though it wont) I'll dupe this –  Mar 27 '17 at 10:23

2 Answers2

0

You don't need to provide date format in the model class.

try this type format:

@Html.TextBoxFor(m => m.AppointmentDate, "{0:dd/MM/yyyy}")
crellee
  • 855
  • 1
  • 9
  • 18
0

Did you remember a define a setup like this?

@using (Html.BeginForm())
{
<div class="form-horizontal">
<div class="form-group">
<div class="input-capsule appointment-date">
 @Html.LabelFor(m => m.AppointmentDate, "Randevu Tarihi:")
        @Html.TextBoxFor(m => m.AppointmentDate, "{0:dd-MM-yyyy HH:mm}", new {@class = "form-control required" })
    </div>
</div>
 <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>
}
crellee
  • 855
  • 1
  • 9
  • 18