jQuery日期不会在后操作中填充模型对象 [英] jquery dates does not populate model object on post action

查看:78
本文介绍了jQuery日期不会在后操作中填充模型对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个剃刀视图:

<div class="panel-body">
    <div class="form-group row">
        <div class="col-sm-6">
            @Html.LabelFor(model => model.DrivingLicenceNo, "Licence No", htmlAttributes: new { @class = "control-label" })
            @Html.EditorFor(model => model.DrivingLicenceNo, new { htmlAttributes = new { @class = "form-control" } })
        </div>

        <div class="col-sm-6">
            @Html.LabelFor(model => model.DrivingLicenceExpiryDate, "Expiry Date", htmlAttributes: new { @class = "control-label" })
            @Html.EditorFor(model => model.DrivingLicenceExpiryDate, new { htmlAttributes = new { @class = "form-control" } })
        </div>
    </div>
    <div class="form-group row">
        <div class="col-sm-6">
            @Html.LabelFor(model => model.DrivingLicenceImage, "Licence Image", htmlAttributes: new { @class = "control-label" })
            <input type="file" name="uploadFile" />
        </div>
        <div class="col-sm-6">
            @Html.LabelFor(model => model.DateofBirth, "Date of Birth", htmlAttributes: new { @class = "control-label" })
            @Html.EditorFor(model => model.DateofBirth, new { htmlAttributes = new { @class = "form-control" } })
        </div>
    </div>
</div>

并显示日期,我正在使用以下脚本:

and to display dates, i am using the following script:

<script>
$(function() {
  $("#DrivingLicenceExpiryDate").datepicker({
      changeMonth: true,
      changeYear: true,
      dateFormat: 'dd-mmm-yyyy'
  });
  $("#DateofBirth").datepicker({
      changeMonth: true,
      changeYear: true,
      dateFormat: 'dd-mmm-yyyy'
  });

});

但是,如果不使用脚本,我会在后操作方法模型中填充日期,但是当我放回脚本时,不会填充模型对象中的日期.我需要显示下拉菜单,以便用户更轻松地选择日期

however without using the script, i get the dates populated in the post action method model but when i put the script back in, the dates in the model object does not get populated. i need to display the drop down menu to make it easier for the user to pick dates

推荐答案

我不确定您的dateFormat是否正确.使用您当前指定的格式,它将在输入字段中设置一个日期,例如"25-088-20162016" .发布表单后,模型联编程序将无法将其设置为DateTime属性,因为该值(或值的格式)看起来不像是正确的日期时间值.

I am not sure your dateFormat is correct. With the format you specified currently, It will set a date like "25-088-20162016" to the input field. When the form is posted, Model binder will not be able to set this to a DateTime property as the value (or the format of the value) does not looks like a correct date time value.

尝试这种格式.

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

或者,如果您坚信格式是有效的日期格式(对于某些有效的区域性),则可以在服务器中设置区域性,以便服务器知道如何处理传入的输入值.

Or If you strongly believe that format is a valid date format (for some valid culture), you can set the culture in the server so that server knows how to process the input value coming in.

添加2个链接以供参考

  1. 全球化元素

您如何在全球范围内工作在ASP.NET中设置日期格式?

这篇关于jQuery日期不会在后操作中填充模型对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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