日期时间未在视图中设置

时间:2011-08-03 10:32:19

标签: asp.net asp.net-mvc entity-framework-4

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

但在控制器操作中,DateOfBirth设置为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' });
    });

我该如何解决?

2 个答案:

答案 0 :(得分:0)

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

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

编辑:

我刚刚回复了您的问题并找到了解决办法。

在viewmodel类IdentifyModel中,您必须将DateOfBirth声明为可为空的DateTime:

public DateTime? DateOfBirth { get; set; }

执行此操作会在您获取视图时停止显示01/01/0001(或任何默认日期)。

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

答案 1 :(得分:0)

你这里有太多的复杂性。您没有显示设置为min的日期。如果DateTime字段永远不为null,则不需要可为空的类型。创建最简单的应用程序,以再现问题。设置断点以找出未显示日期的原因。