jquery - BootStrap Datepicker 从 View 到 Controller 返回 null

标签 jquery asp.net-mvc-4 date datepicker

首先我解释一下我的问题,我们正在使用 BootStrap DatePicker,提交 Html.BeginForm 后,它会转到带有选择日期的 Controller 并更新我的开发系统(pc)中的数据库,但相同的编码在测试服务器中不起作用,并且客户端系统。在测试服务器上调试后,我发现日期选择器向 Controller 返回 NULL 当我选择超过 12 天的日期,例如 (13-10-2014),而对于少于 12 天的日期,例如 (12-10-2014) 则工作正常。

changed the culture作为 Invariant 但仍然向 Controller 返回 null 值

在 BeginForm 中定义的日期选择器,其他控件返回正确的值,期望 DatePicker。

Html.BeginForm("MergeTeacherDetails","Profile", FormMethod.Post, new { id = "FormTeacherDetails" })))

日期选择器格式更改为 ('dd/mm/yyyy') 这里查看代码

@Html.LabelFor(m => m.DOB, "DOB", new { @class = "control-label"})
<div class="controls">
 @{System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
   @Html.TextBoxFor(model => model.DOB, new { placeholder = "DOB",@readonly = "readonly",@class="ReadOnly" })}
</div>

这里是 JavaScript

$('#DOB').datepicker({ startView: 2, autoclose: true, format: "dd/mm/yyyy", endDate: '@DateTime.Now.ToString("dd/MM/yyyy")', changeYear: true, changeMonth: true, weekStart: 1 });

这里是 DAL 代码

 public Nullable<System.DateTime> DOB { get; set; }

这里是 Controller 代码

public ActionResult MergeTeacherDetails(Staffs objStaffs)
    {

         int res = _staffRepository.InsertOrUpdate(objStaffs);
         if(res>0)
         {
          _mapStaffRepository.SaveMapStaff(objStaffs.StaffId);
         }
         return RedirectToAction("/MyProfile/1");
    }

在 Controller 参数 objStaffs.DOB 中返回 NULL

这个

请帮我解决这个问题

提前致谢

最佳答案

使用模型绑定(bind)器将发布的值绑定(bind)到日期时间值时(将发布的值绑定(bind)到 objStafss 模型时),您需要确保应用程序使用预期的日期格式

默认情况下,模型绑定(bind)器将使用当前的区域性格式,在您的计算机上可能为 dd/mm/yyyy 而在测试/客户端计算机上将为 mm/dd/yyyy.

您可以创建始终需要格式为 dd/mm/yyyy 的模型绑定(bind)器(在 .net 中表示为 dd/MM/yyyy):

public class CustomModelBinder : DefaultModelBinder
{
    protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext,
        PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
    {
        var propertyType = propertyDescriptor.PropertyType;         
        if(propertyType == typeof(DateTime) || propertyType == typeof(DateTime?))
        {
            var providerValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            if (null != providerValue)
            {
                DateTime date;
                if (DateTime.TryParseExact(providerValue.AttemptedValue, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
                {
                    return date; 
                }                    
            }
        }
        return base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
    }
}

然后,您需要告诉 MVC 使用您的模型绑定(bind)器,例如,您可以通过替换默认绑定(bind)器,在 global.asax Application_Start 事件中为您的应用程序全局连接它:

ModelBinders.Binders.DefaultBinder = new CustomModelBinder();

关于jquery - BootStrap Datepicker 从 View 到 Controller 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26711248/

相关文章:

jquery - 对 jQuery $.get 和 $.load 感到困惑

javascript - 网格 JavaScript 中的随机框

jQuery 传递 CSS 变量

asp.net-mvc - ASP.NET MVC 4 - 异常处理不起作用

java - date.getTime() 比 System.getCurrentTimeMillis() 短

JQuery 循环不选取选中的复选框

.net - Entity Framework 与表示层分离

asp.net-mvc - 未找到局部 View 'First.cshtml' 或没有 View 引擎支持搜索的位置

python - 及时格式化为小数点后 3 位数字

java - 是否可以覆盖 DateFormat.getDateTimeInstance()?