2011-02-17 73 views
13

我已经决定开始使用MVC 3,并且在尝试将我的一个Web应用程序重做为MVC3时遇到了此问题。Textboxfor 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; } 
} 

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

<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/Jan/0001 00:00:00。 所以首先:我怎样才能让日期格式化为shortdatestring? 我找到了MVC2 solution,建议在SharedFolder中创建EditorTemplates文件夹,并使用EditorFor代替,但这似乎不适用于MVC3。它似乎也可以防止像TextBox一样容易地添加类。

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

回答

8

为您编辑模板试试这个

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

来源:MVC 3 Editor Template with DateTime

jQuery的日期时间比较

$('#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 date comparison

+0

感谢您的联系!不过,我的日期仍然显示01/01/001。但我想我可以用jquery重写。 – LanFeusT 2011-02-18 01:50:53

+0

可能是一个愚蠢的问题,但是被发送到视图之前填充日期? – DarkStar33 2011-02-18 15:17:28

0

// datimetime在日期选择器显示为11/24/2011 12:00:00 AM

//你可以通过空间分割这个并将其值设置迄今为止只有

脚本:

if ($("#StartDate").val() != '') { 
     var arrDate = $('#StartDate').val().split(" "); 
     $('#StartDate').val(arrDate[0]); 
    } 

标记:

<div class="editor-field"> 
     @Html.LabelFor(model => model.StartDate, "Start Date") 
     @Html.TextBoxFor(model => model.StartDate, new { @class = "date-picker-needed" }) 
    </div> 

希望这有助于..

6

这样做有一个相当简单的方法。只需使用一个TextBox而非TextBoxFor

@Html.TextBox("ReturnDepartureDate", 
    Model.ReturnDepartureDate.ToString("dd/MM/yyyy")) 

这有将所有的不显眼的JavaScript元素等也少代码的优势!

2

如果你用以下装饰你的viewmodel对象,你只能看到文本框中的日期部分。

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")] 
public DateTime Date { get; set; } 

那么你的页面上,你可以简单地做以下

@Html.EditorFor(model => model.Date) 

注意,这是使用标准MVC3。我没有自定义日期编辑器模板。

此答案一切归功于assign date format with data annotations

3

看一看这个例子

@Html.TextBoxFor(s => s.dateofbirth, new { Value = @Model.dateofbirth.ToString("dd/MM/yyyy") }) 
0

太晚了,但可能是它可以帮助别人:

型号:

[Display(Name = "Date of birth")] 
public DateTime? DOB{ get; set; } 

HTML:

@Html.TextBoxFor(m => m.DOB, "{0:MM/dd/yyyy}", 
      new { @placeholder="MM/DD/YYYY", @au19tocomplete="off" }) 

JavaScript的日历添加到控件:

$('#DOB').datepicker({changeMonth: true, changeYear: true, 
     minDate: '01/01/1900', maxDate: new Date});