2013-03-26 88 views
0

我使用MVC4开发Intranet网络应用程序。当我尝试创建一个新的时,我必须选择一个开始日期。为了做到这一点,我使用jQuery脚本实现了一个日期选择器。我在文化和小数方面遇到了问题,但幸运的是,我帮助解决了问题。接受的日期格式为yyyy/mm/dd格式,但我想使其为dd/mm/yyyy。可能吗?我试图改变格式,包括标签,...没有效果。MVC4错误日期格式

创建视图:

@model BuSIMaterial.Models.Person 
@{ 
    ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 

@using (Html.BeginForm()) { 
    @Html.ValidationSummary(true) 

    <fieldset> 
     <legend>Person</legend> 

     <div class="editor-label"> 
      First name : 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(model => model.FirstName, new { maxlength = 50 }) 
      @Html.ValidationMessageFor(model => model.FirstName) 
     </div> 

     <div class="editor-label"> 
      Last name : 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(model => model.LastName, new { maxlength = 50 }) 
      @Html.ValidationMessageFor(model => model.LastName) 
     </div> 

     <div class="editor-label"> 
      National number : 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(model => model.NumNat, new { maxlength = 11 }) 
      @Html.ValidationMessageFor(model => model.NumNat) 
     </div> 

     <div class="editor-label"> 
      Start date : 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(model => model.StartDate, new {@class = "datepicker" }) 
      @Html.ValidationMessageFor(model => model.StartDate) 
     </div> 

     <div class="editor-label"> 
      End date : 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(model => model.EndDate, new { @class = "datepicker" }) 
      @Html.ValidationMessageFor(model => model.EndDate) 
     </div> 

     <div class="editor-label"> 
      Distance House - Work (km) : 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.HouseToWorkKilometers) 
      @Html.ValidationMessageFor(model => model.HouseToWorkKilometers) 
     </div> 

     <div class="editor-label"> 
      Category : 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("Id_ProductPackageCategory", "Choose one ...") 
      @Html.ValidationMessageFor(model => model.Id_ProductPackageCategory) <a href = "../ProductPackageCategory/Create">Add a new category?</a> 
     </div> 

     <div class="editor-label"> 
      Upgrade? : 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Upgrade) 
      @Html.ValidationMessageFor(model => model.Upgrade) 
     </div> 

     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
    @Scripts.Render("~/bundles/jqueryui") 
    @Styles.Render("~/Content/themes/base/css") 
} 

的jQuery选择器和全球化文化:

$(document).ready(function() { 
    $(".datepicker").datepicker({ 
         changeMonth: true, 
         changeYear: true, 
     dateFormat: 'dd/mm/yy' 

    }); 
}); 

$.validator.methods.number = function (value, element) { 
    return this.optional(element) || 
       !isNaN(Globalize.parseFloat(value)); 
} 
$(document).ready(function() { 
    Globalize.culture('fr-FR'); 
}); 

jQuery.extend(jQuery.validator.methods, { 
    range: function (value, element, param) { 
     //Use the Globalization plugin to parse the value   
     var val = $.global.parseFloat(value); 
     return this.optional(element) || (
        val >= param[0] && val <= param[1]); 
    } 
}); 

非常感谢。

+0

你可能想在'DateTime'类型属性的编辑模板来写这样的代码,然后只需用'Html.EditorFor',而不是'Html.TextBoxFor '这些属性。这将使您的代码保持干爽。 – 2014-05-06 09:40:27

回答

1

你应该为模型属性添加属性

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")] 
public DateTime StartDate { get; set; }