2011-08-03 54 views
0

没有设置我有这部分在asp.net MVC 3视图:日期时间鉴于

<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> 

但在控制器动作出生日期设为Datetime.minimum值

public ActionResult Index(IdentifyModel identifyModel) {} 

视图代码

@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' }); 
    }); 

何w我可以修复它

+0

你的问题不清楚 –

回答

0

您需要有DateOfBirth作为DateTime?数据类型;可空的日期时间。这意味着如果DateOfBirth没有值,那么它将是NULL,而不是DateTime.MinValue

public class IdentifyModel { 
    public DateTime? DateOfBirth {get;set;} 
} 

编辑:

我只是replciated您的问题,并找到了解决。

在您的视图模型类IdentifyModel您必须声明出生日期为null的日期时间:

public DateTime? DateOfBirth { get; set; } 

这样做停止的观点从显示01/01/0001(或任何默认的日期是),当你的看法。

然后,当我在文本框中输入“1/2/2010”并发布窗体时,DateTime值“1/2/2010”被重新发布到视图模型中。

+0

嗨贾森:重点是,即使我在文本框中输入日期,我没有得到它在控制器。 – DotnetSparrow

+0

您可以发布您的视图的所有代码吗? –

+0

请看我的问题。我已添加查看代码 – DotnetSparrow

0

这里你太复杂了。您不显示日期被设置为最小。如果DateTime字段永远不为null,则不需要可为空的类型。创建可再现问题的最简单的应用程序。设置一个断点来找出为什么不显示日期。