开发者

DatePicker format

I am using datepicker from jQuery. Once selected, I want开发者_Go百科 it to display as 9Feb, 11. However when saving I want it to convert it to 2/9/11. How can I do that?

When re-displaying the dte, it should display 9Feb, 11 again. How can I do that?

My piece of code is as follows:

@Html.TextBoxFor(m => (Model.dDay.Date))

 $("#dDay_Date").datepicker({ dateFormat: 'dd-M-yy' });


You can use the 'altField' option to populate a hidden field in your form, just change the date field to hidden: <input type="hidden" name="date" id="date" value=""/>. You also set the format of the alt field with 'altFormat':

$("#date_display").datepicker({
    dateFormat: 'd M y', 
    altField: '#date',
    altFormat: 'dd-M-yy'
});


You can add a hidden input field to the page. Using the altField and altFormat options you can send the data to you page in an alternative format.

To do this, you can create a custom editor template for the DateTime format. Add the file DateTime.cshtml to the Views/Shared/EditorTemplates folder.

You can then use a contents like this (not tested):

@model DateTime?
@using System.Globalization
@{
    var name = MvcHtmlString.Create(ViewData.TemplateInfo.GetFullHtmlFieldName(""));
    var id = MvcHtmlString.Create(ViewData.TemplateInfo.GetFullHtmlFieldId(""));
    var visibleID = id + "_editor";

    var value = Model ?? DateTime.Today;
    var formattedValue = value.ToString("dd MMM yyyy");

}
<input type="text" id="@(visibleID)" value="@formattedValue" />
<input type="hidden" id="@(id)" name="@(name)" value="@value.ToString("yyyy/MM/dd")" />
<script type='text/javascript'>
    $(function () { $('#@(visibleID)').datepicker({ dateFormat: 'd mm y', altField='@id', altFormat='d/m/y' }); });
</script>

Then you only have to use the TextBoxFor, any DateTime field will have the datetimepicker activated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜