Textboxfor MVC3的日期格式和日期验证 [英] Textboxfor mvc3 date formatting and date validation

查看:213
本文介绍了Textboxfor MVC3的日期格式和日期验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经决定要开始使用MVC 3,我就遇到了这个问题,而试图重做我的网络应用程序MVC3之一。

I've decided to get started with MVC 3 and I ran into this issue while trying to redo one of my web apps to MVC3.

我有项目的设置是这样的:

I've got projects setup this way:

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

和我的报名表的日期这样写的:

And my entry form is written this way for the dates:

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

我的问题是,文本框现在显示01 /月/ 0001 00:00:00。 因此,首先:我怎样才能获得的日期被格式化为shortdatestring? 我发现一个<一个href="http://stackoverflow.com/questions/2088139/how-to-format-the-value-in-a-strongy-typed-view-when-using-asp-net-mvcs-html-tex">MVC2解决方案该建议在SharedFolder创建一个EditorTemplates文件夹,使用EditorFor而是但这并不似乎与MVC3工作。这似乎也prevent轻松添加类,比如它与TextBoxFor

My problem is that the textbox are now displaying 01/Jan/0001 00:00:00. So first: how can I get the date to be formated to shortdatestring? I've found an MVC2 solution which advised to create an EditorTemplates folder in the SharedFolder and use EditorFor instead but that did not seem to be working with MVC3 . It also seemed to prevent adding classes easily like it is with TextBoxFor

其次,一旦被固定的我有,我需要制定一个特殊的验证系统。我需要有结束日期是开始日期之后。我想这样做的检查有一个JavaScript我发现 http://www.datejs.com 但有没有办法做直接验证上我的课?

And secondly once that is fixed I've got a special validation system that I need to put in place. I need to have the End Date to be AFTER the Start Date. I'm thinking of doing the check with a javascript i found http://www.datejs.com but is there a way to do the validation directly on my class?

推荐答案

有关你的编辑模板试试这个

For your edit template try this

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

来源: MVC 3模板编辑器使用日期时间

jQuery的日期时间比较

jQuery DateTime comparisons

$('#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('');
            }
        });

来源: jQuery的日期比较

这篇关于Textboxfor MVC3的日期格式和日期验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆