Ich möchte nur das Datum im Format ddMMyyyy während der Übermittlung akzeptieren. Aber es gibt einen Fehler wieMVC Validierung, um DateTime im Format ddMMyyyy zu akzeptieren
Das Feld FromDate muss ein Datum sein.
Aber, wenn ich Datum setzen in MMTTJJJJ akzeptieren es roperly pkkttrfg.
Mein Modell ist
public class CompareModel
{
[Required]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime FromDate { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime TODate { get; set; }
}
My View Part
<div class="form-group">
@Html.LabelFor(model => model.FromDate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FromDate)
@Html.ValidationMessageFor(model => model.FromDate)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TODate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TODate)
@Html.ValidationMessageFor(model => model.TODate)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
Sie bei Verwendung von Jquery globalisieren oder ein Verfahren zum '$ .validator' nach [diese Antwort aussehen kann hinzufügen ] (http://stackoverflow.com/questions/27285458/jquery-ui-datepicker-and-mvc-view-model-type-datetime/27286969#27286969) - beachten Sie, dass die Lösung für die jquery ui datepicker aber angepasst werden könnte für ein Standard-Textfeld (zB den Wert in seine Komponenten aufteilen, ein neues Javascript 'Date()' -Objekt konstruieren und dessen Gültigkeit testen) –