I have this part in asp.net MVC 3 view:
<tr>
<td>@Html.LabelFor(x => x.DateOfBirth, new { @class = "lbl" }, "Date Of Birth") </td>
<td>@Html.TextBoxFor(x => x.DateOfBirth, new { @class = "w100 _dob" })
<br>@Html.ValidationMessageFor(x => x.DateOfBirth)
</td>
</tr>
but in controller action DateOfBirth is set to Datetime.minimum value
public ActionResult Index(IdentifyModel identifyModel) {}
VIEW CODE
@model CreditRegistry.Models.Helpers.IdentifyModel
@{
ViewBag.Title = "Identify";
ViewBag.BreadCrumb = "Identify";
}
@section BodyTitle {
<span>Identify</span> yourself
}
<ul class="extra">
<li><a href="#">Receive Free Credit Report</a></li>
<li><a href="#">Premium Subscription</a></li>
<li><a href="#">FAQ</a></li>
</ul>
@using (Html.BeginForm("Index","Identify",FormMethod.Post,new{autocomplete="off", id="frmIdentify"})) {
<p class="info">
View and print your credit report after you authenticate your identity. <u><b>Your first
credit report is free.</b></u>
</p>
<p>
Please provide as much information as you can so we can identify who you are.
Refer to the <b>@Html.ActionLink("FAQ", "FAQ", "Home")</b> for help.
</p>
@*
<p>
Is this report for an @Html.RadioButton("_mode", "_individualFields", true, new { @class = "_mode" })
Individual or a @Html.RadioButton("_mode", "_businessFields", new { @class = "_mode" })
Business?
</p>
*@
@Html.ValidationSummary()
<fieldset class="form">
<table>
<tr>
<td>@Html.LabelFor(x => x.FirstName, new { @class = "lbl" }, "First Name")</td>
<td>@Html.TextBoxFor(x => x.FirstName, new { @class = "w200" })
<br>@Html.ValidationMessageFor(x => x.FirstName)
</td>
</tr>
<tr>
<td>@Html.LabelFor(x => x.LastName, new { @class = "lbl" }, "Last Name")</td>
<td>@Html.TextBoxFor(x => x.LastName, new { @class = "w200" })
<br>@Html.ValidationMessageFor(x => x.LastName)
</td>
</tr>
<tr>
<td>@Html.LabelFor(x => x.DateOfBirth, new { @class = "lbl" }, "Date Of Birth") </td>
<td>@Html.TextBoxFor(x => x.DateOfBirth, new { @class = "w100 _dob" })
<br>@Html.ValidationMessageFor(x => x.DateOfBirth)
</td>
</tr>
<tr>
<td>(optional)@Html.LabelFor(x => x.Phone, new { @class = "lbl" }, "Mobile Number") </td>
<td>@Html.TextBoxFor(x => x.Phone, new { @class = "w200" })
<br>@Html.ValidationMessageFor(x => x.Phone)
</td>
</tr>
<tr>
<td>(optional)@Html.LabelFor(x => x.DLNumber, new { @class = "lbl" }, "Driver's License Number") </td>
<td>@Html.TextBoxFor(x => x.DLNumber, new { @class = "w200" })
<br>@Html.ValidationMessageFor(x => x.DLNumber)
</td>
</tr>
<tr>
<td>(optional)@Html.LabelFor(x => x.PassportNo, new { @class = "lbl" }, "Passport Number") </td>
<td>@Html.TextBoxFor(x => x.PassportNo, new { @class = "w200" })
<br>@Html.ValidationMessageFor(x => x.PassportNo)
</td>
</tr>
<tr>
<td>(optional)@Html.LabelFor(x => x.NationalID, new { @class = "lbl" }, "National ID") </td>
<td>@Html.TextBoxFor(x => x.NationalID, new { @class = "w200" })
<br>@Html.ValidationMessageFor(x => x.NationalID)
</td>
</tr>
</table>
<p>
<label>
</label>
<input type="submit" value="Start Identification" />
</p>
</fieldset>
}
<script type="text/javascript">
$(function () {
$('#frmIdentify').attr('autocomplete', 'off'); @*make sure autocomplete off for browsers that already stored some information*@
$('._dob').datepicker({ dateFormat: "yy-mm-dd", changeMoth: true, changeYear: true, yearRange: '-100y:c+nn' });
});
How can I fix it
You need to have
DateOfBirthas aDateTime?datatype; a Nullable DateTime. This means that ifDateOfBirthhas no value, then it will be NULL, rather thenDateTime.MinValue.EDIT:
I just replciated your issue and found a fix.
In your viewmodel class
IdentifyModelyou must declare DateOfBirth as a nullable DateTime:Doing this stops the view from showing 01/01/0001 (or whatever the default date is) when you GET the view.
Then, when I entered “1/2/2010” into the textbox and posted the form, the DateTime value “1/2/2010” was posted back into the viewmodel.