I am building an MVC4 app and in my model I have these fields:
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime mDateCreated { get; set; }
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime? mDateModified { get; set; }
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime? mDateLastDisplayed { get; set; }
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime mStartDate { get; set; }
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime? mEndDate { get; set; }
I am trying to set datepickers in a view. Here's what I have done so far:
<div class="float-left">
<p>Date Created:</p>
<p>Date of last modification:</p>
<p>Date Last Displayed:</p>
<p>Date of promotion's start:</p>
<p>Date of promotion's end:</p>
</div>
<div class="float-right">
<p>
@Html.TextBoxFor(_item => _item.mCardOfTheDay.mDateCreated, new
{
@value = Model.mCardOfTheDay.mDateCreated,
@selectedDate = Model.mCardOfTheDay.mDateCreated,
@class = "datePick",
@type = "date",
@id = "dateCreated"
})
</p>
<p>
@(Model.mCardOfTheDay.mDateModified != null ? Html.DisplayFor(_item => _item.mCardOfTheDay.mDateModified) : Html.Label(ValueDomain.FIELD_UNAVAILABLE))
</p>
<p>@(Model.mCardOfTheDay.mDateLastDisplayed != null ? Html.DisplayFor(_item => _item.mCardOfTheDay.mDateLastDisplayed) : Html.Label(ValueDomain.FIELD_UNAVAILABLE))</p>
<p>
@*@Html.EditorFor(_item => _item.mCardOfTheDay.mStartDate, new { @class = "datePick", @type="date" })*@
@*@Html.TextBoxFor(_item => _item.mCardOfTheDay.mStartDate, new {@id = "dateStart"})*@
@* @Html.TextBoxFor(_item => _item.mCardOfTheDay.mStartDate, new
{
@value = (DateTime?) Model.mCardOfTheDay.mStartDate,
@selectedDate = (DateTime?) Model.mCardOfTheDay.mStartDate,
@class = "datePick",
@type = "date",
@id = "dateStart"
})*@
</p>
<p>
@Html.TextBoxFor(_item => _item.mCardOfTheDay.mEndDate, new
{
@value = Model.mCardOfTheDay.mEndDate,
@selectedDate = Model.mCardOfTheDay.mEndDate,
@class = "datePick",
@type = "date",
@id = "dateEnd"
})
</p>
</div>
<div class="clear"></div>
And CSS to show what I'm doing with the classes:
$('.datePick').datepicker({
dateFormat: "dd/ww/yy",
});
$('.datePick').each(function () {
var a = $(this).datepicker({
dateFormat: "dd/ww/yy",
defaultDate: new Date($(this).val())
});
});
This shows the various problems I have. First:
- Using
Html.TexborFor
helpers with@value
,@selectedDate
and so on does displays a datepicker, but this datepicker's default shown value isaaaa-mm-dd
instead of the value binded to the model and when I pass the model to the controller none of the data is kept (meaning that the mEndDate is always null); - Trying only to set a datepicker ends up with the same results;
- Here's a sample of Html code behind to show you the "results" I have:
<input class="input-validation-error datePick" data-val="true" data-val-date="The field mDateCreated must be a date." data-val-required="The mDateCreated field is required." id="dateCreated" name="mCardOfTheDay.mDateCreated" selectedDate="11/21/2013 00:00:00" type="date" value="" />
- And if I use the EditorFor, all I have is a date in string format, no datepicker.
What I want is a datepicker with the proper date selected that passed the selected date to the controller method in post. Can anyone help me figure out why this does not work?