【问题标题】:BootStrap Datepicker return null from View to ControllerBootStrap Datepicker 从视图返回 null 到控制器
【发布时间】:2014-11-03 09:27:51
【问题描述】:

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

changed the culture 为 Invariant 但仍将 null 值返回给控制器

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

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

DatePicker 格式更改为 ('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; }

这里是控制器代码

public ActionResult MergeTeacherDetails(Staffs objStaffs)
    {

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

在控制器参数 objStaffs.DOB 中返回为 NULL

这个

请帮我解决这个问题

提前致谢

【问题讨论】:

  • 这个编码中的问题是没有 ViewModel。如果我想创建 ViewModel,在整个项目中处理它非常复杂
  • 你能发布控制器代码吗?
  • 你能帮我解决这个问题吗?

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


【解决方案1】:

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

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

您可以创建您的模型绑定器,该绑定器始终需要格式为 dd/mm/yyyy(在 .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 使用您的模型绑定器,例如,您可以通过替换默认绑定器在 global.asax Application_Start 事件中为您的应用程序全局连接它:

ModelBinders.Binders.DefaultBinder = new CustomModelBinder();

【讨论】:

  • 感谢您的回复,此代码工作正常,但是当我选择日期(2014 年 10 月 30 日)时,它会将该日期转换为(2014 年 1 月 30 日)。您是否在当地检查过这种情况
  • 您还需要确保将现有值呈现到文本框时使用的格式是dd/mm/yyyy。您可以使用 [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] 中的 DisplayFormatAttribute,但这仅适用于 EditorFor 而不适用于 TextBoxFor。您可以创建一个自定义助手,如 this one 或您自己的日期时间编辑器模板
  • 如果您使用的是 MVC 5.1+,您可以添加 DisplayFormat 属性并更改您的代码以使用 EditorFor,同时仍然使用 @Html.EditorFor(model =&gt; model.Dob, new { htmlAttributes = new { placeholder = "DOB", @readonly = "readonly", @class = "ReadOnly" } }) 设置 html 属性(如占位符或只读)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
  • 1970-01-01
  • 2017-02-13
  • 2020-02-18
  • 2015-10-15
  • 1970-01-01
相关资源
最近更新 更多