textboxfor mvc3日期格式和日期验证

Lan*_*usT 13 .net asp.net-mvc asp.net-mvc-3

我决定开始使用MVC 3,并且在尝试将我的一个Web应用程序重做为MVC3时遇到了这个问题.

我有这样设置的项目:

public class Project
{
    public int      ProjectID       { get; set; }

    [Required(ErrorMessage="A name is required")]
    public string   Name            { get; set; }

    [DisplayName("Team")]
    [Required(ErrorMessage="A team is required")]
    public int      TeamId          { get; set; }

    [DisplayName("Start Date")]
    [Required(ErrorMessage="A Start Date is required")]
    [DisplayFormat(ApplyFormatInEditMode=true, DataFormatString="{0:d}")]
    public DateTime StartDate       { get; set; }

    [DisplayName("End Date")]
    [Required(ErrorMessage="An End Date is required")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
    public DateTime EndDate         { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的报名表是这样编写的日期:

<table>
    <tr>
        <td>
            <div class="editor-label">
                @Html.LabelFor(model => model.StartDate)
            </div>
        </td>
        <td>
            <div class="editor-label">
                @Html.LabelFor(model => model.EndDate)
            </div>
        </td>
    </tr>
    <tr>
        <td>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.StartDate, new { @class = "datepicker", id="txtStartDate" })
                @Html.ValidationMessageFor(model => model.StartDate)
            </div>
        </td>
        <td>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.EndDate, new { @class = "datepicker", id = "txtEndDate" })
                @Html.ValidationMessageFor(model => model.EndDate)
            </div>
        </td>
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

我的问题是文本框现在显示01/Jan/0001 00:00:00.首先:我怎样才能将日期格式化为shortdatestring?我发现了一个MVC2解决方案,建议在SharedFolder中创建一个EditorTemplates文件夹,然后使用EditorFor,但这似乎不适用于MVC3.它似乎也阻止了像TextBoxFor一样容易添加类

其次,一旦修复,我就有了一个特殊的验证系统,我需要将其安装到位.我需要将结束日期设置为开始日期之后.我正在考虑使用javascript进行检查我发现http://www.datejs.com但有没有办法直接在我的课上进行验证?

小智 8

对于您的编辑模板,试试这个

@model DateTime?
@Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datePicker" })
Run Code Online (Sandbox Code Playgroud)

来源:带有DateTime的MVC 3编辑器模板

jQuery DateTime比较

$('#txtbox').change(function() {
            var date = $(this).val();
            var arrDate = date.split("/");
            var today = new Date();
            useDate = new Date(arrDate[2], arrDate[1] -1, arrDate[0]);

            if (useDate > today) {
                alert('Please Enter the correctDate');
                $(this).val('');
            }
        });
Run Code Online (Sandbox Code Playgroud)

来源:Jquery日期比较


Lia*_*iam 6

有一个相当简单的方法来做到这一点.只使用一个TextBox而不是一个TextBoxFor

@Html.TextBox("ReturnDepartureDate", 
    Model.ReturnDepartureDate.ToString("dd/MM/yyyy"))
Run Code Online (Sandbox Code Playgroud)

这样做的好处是可以添加所有不引人注目的JavaScript元素等.还有更少的代码!