2013-03-14 70 views
3

我刚刚实现了jQuery UI提供的datepicker,以便使用日历来选择日期。不幸的是,当我选择日期并希望点击创建按钮时,应用程序不想保存我的条目,因为日期格式无效。我试图在webconfig中添加全球化参数,但似乎这种方式不起作用。MVC日期格式和日期选择器

jQuery的部分:

@section Scripts { 
@Scripts.Render("~/bundles/jqueryval") 
@Scripts.Render("~/bundles/jqueryui") 
@Styles.Render("~/Content/themes/base/css") 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $(".datepicker").datepicker({ 
      changeMonth: true, 
      changeYear: true, 

     }); 
    }); 
</script> 

}

我创建视图:

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

任何想法来解决这个问题呢?

编辑:当我修改并把我的日期格式为“dd/mm/yy”格式时,出现验证错误,说“该字段StartDate必须是日期。”如果我将格式更改为“mm/dd/yy”格式,则会出现一条验证错误消息,说明“该字段StartDate必须是日期。”

+0

我有同样的问题。你找到解决方案吗? – StefanG 2016-01-16 15:31:28

回答

1

你可以指定日期的格式,使用dd/MM/yyyy格式尝试这样的事情,样品:

$(".datepicker").datepicker({ 
    changeMonth: true, 
    changeYear: true, 
    dateFormat: 'dd/MM/yyyy' 
}); 

而当你选择一个日期,在文本框中的值将在该格式冲击片雷管和在你的帖子上提交。

+0

感谢您的帮助!我检查了这个链接(http://docs.jquery.com/UI/Datepicker/formatDate)来选择我的格式,我发现它...但似乎我的应用程序不接受“dd/mm/yy”格式,通过说“该字段StartDate必须是日期。” – Traffy 2013-03-14 15:26:52

+0

你可以发布一部分输出代码吗?可能它是jQuery Unobtrusive验证。 – 2013-03-14 15:35:28

+0

当然,输出代码是什么意思? – Traffy 2013-03-14 15:40:21

3

你可以为你的模型格式可能更改为类似:

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

编辑:

或更新从对方的回答这个代码:

$(".datepicker").datepicker({ 
    changeMonth: true, 
    changeYear: true, 
    dateFormat: 'dd-mm-yy' 
}); 
+0

感谢您的回答。我现在再次尝试过,但我的日期(例如:2013年3月30日)未被接受为日期。 – Traffy 2013-03-14 15:39:22

+0

您是否尝试过我更新的代码? – 2013-03-14 16:30:30

+0

我试过了,但不幸的是它没有工作。当我把“dateFormat:'dd-mm-yy',它说”字段StartDate必须是日期。“,当我把它作为”dateFormat:mm-dd-yy“时,它说我的日期不被接受为一个日期...我该怎么做? – Traffy 2013-03-15 10:06:28

0

这可以帮助你,你可以改变你的Global.asax文件中的当前文化,对于应用程序级别例如,

using System.Globalization; 
using System.Threading; 

protected void Application_BeginRequest(Object sender, EventArgs e) 
{  
    CultureInfo newCulture = (CultureInfo) System.Threading.Thread.CurrentThread.CurrentCulture.Clone(); 
    newCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy"; 
    newCulture.DateTimeFormat.DateSeparator = "-"; 
    Thread.CurrentThread.CurrentCulture = newCulture; 
} 
-1

你有没有像jquery.validator.js的插件? 如果你...试试这个:

$("#myform").validate({ 
    ignore: "#DateInput" 
}) 
+2

禁用验证来解决错误似乎是一个坏主意。解决错误可能会更好。 – DVK 2016-06-13 18:58:59