Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Will Nick Clarke's answer work when you submit the values on postback? </p> <p>In MVC2 preview 2, calling Html.Textbox("abc", Model.ToString()) will render a textbox with ".abc" appended to the name, e.g. </p> <pre><code>&lt;input id="StartDate_abc" name="StartDate.abc" type="text" value="02 Feb 09" /&gt; </code></pre> <p>which will cause problems when you postback and attempt to UpdateModel().</p> <p>I did an editor template for a DateTime, the following works for me:</p> <p>/Views/Shared/EditorTemplates/DateTime.ascx:</p> <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;DateTime&gt;" %&gt; &lt;%= Html.TextBox(String.Empty, Model.ToString("dd MMM yy")) %&gt; </code></pre> <p>or, to use jQuery's DatePicker for all your DateTimes add a reference to jQuery and jQueryUI to either your Masterpage or to the View containing the call to EditorFor.</p> <p>/Views/Shared/EditorTemplates/DateTime.ascx:</p> <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;DateTime&gt;" %&gt; &lt;%= Html.TextBox("", Model.ToString("dd MMM yy")) %&gt; &lt;script type="text/javascript"&gt; $("#&lt;%= ViewData.ModelMetadata.PropertyName %&gt;").datepicker({ dateFormat: 'dd M y' }); &lt;/script&gt; </code></pre> <p><strong>Update: ASP.NET MVC3</strong>, using the Razor syntax:</p> <pre><code>@model System.DateTime @Html.TextBox("", Model.ToString("dd MMM yy")) &lt;script type="text/javascript"&gt; $("#@ViewData.ModelMetadata.PropertyName").datepicker({ dateFormat: 'dd M y' }); &lt;/script&gt; </code></pre> <p>And to use it all you need in your View is:</p> <pre><code>@Html.EditorFor(model =&gt; model.DueDate) </code></pre> <p>-Matt</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