Note that there are some explanatory texts on larger screens.

plurals
  1. POTextboxfor mvc3 date formatting and date validation
    text
    copied!<p>I've decided to get started with MVC 3 and I ran into this issue while trying to redo one of my web apps to MVC3.</p> <p>I've got projects setup this way:</p> <pre><code>public class Project { public int ProjectID { get; set; } [Required(ErrorMessage="A name is required")] public string Name { get; set; } [DisplayName("Team")] [Required(ErrorMessage="A team is required")] public int TeamId { get; set; } [DisplayName("Start Date")] [Required(ErrorMessage="A Start Date is required")] [DisplayFormat(ApplyFormatInEditMode=true, DataFormatString="{0:d}")] public DateTime StartDate { get; set; } [DisplayName("End Date")] [Required(ErrorMessage="An End Date is required")] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")] public DateTime EndDate { get; set; } } </code></pre> <p>And my entry form is written this way for the dates:</p> <pre><code>&lt;table&gt; &lt;tr&gt; &lt;td&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.StartDate) &lt;/div&gt; &lt;/td&gt; &lt;td&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.EndDate) &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;div class="editor-field"&gt; @Html.TextBoxFor(model =&gt; model.StartDate, new { @class = "datepicker", id="txtStartDate" }) @Html.ValidationMessageFor(model =&gt; model.StartDate) &lt;/div&gt; &lt;/td&gt; &lt;td&gt; &lt;div class="editor-field"&gt; @Html.TextBoxFor(model =&gt; model.EndDate, new { @class = "datepicker", id = "txtEndDate" }) @Html.ValidationMessageFor(model =&gt; model.EndDate) &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>My problem is that the textbox are now displaying 01/Jan/0001 00:00:00. So first: how can I get the date to be formated to shortdatestring? I've found an <a href="https://stackoverflow.com/questions/2088139/how-to-format-the-value-in-a-strongy-typed-view-when-using-asp-net-mvcs-html-tex">MVC2 solution</a> which advised to create an EditorTemplates folder in the SharedFolder and use EditorFor instead but that did not seem to be working with MVC3 . It also seemed to prevent adding classes easily like it is with TextBoxFor</p> <p>And secondly once that is fixed I've got a special validation system that I need to put in place. I need to have the End Date to be AFTER the Start Date. I'm thinking of doing the check with a javascript i found <a href="http://www.datejs.com" rel="nofollow noreferrer">http://www.datejs.com</a> but is there a way to do the validation directly on my class?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload