2014-11-03 30 views
1

第一我解释我的问题,我们使用BootStrap DatePicker,提交后Html.BeginForm它去控制器与选择日期和更新数据库在我的开发系统(PC),但相同编码在测试服务器和客户端系统中不起作用。在测试服务器上调试后,我发现日期选择器返回NULL到控制器 当我选择日期超过12天例如(13-10-2014)和小于12日期像(12-10-2014)工作正常。BootStrap Datepicker返回null从视图到控制器

changed the culture为不变,但仍其返回空值到控制器

的BeginForm内定义的日期选择器,其他控件返回正确的价值观期待的DatePicker。

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

的DatePicker的格式改变为( '日/月/年') 这里查看代码

@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

请帮我提前解决这个

感谢

+0

在这个编码的问题是没有视图模型。如果我想创建ViewModel,它非常复杂,可以在整个项目中处理 – ManiMaranVasan 2014-11-03 10:05:09

+0

您可以发布控制器代码吗? – freshbm 2014-11-03 10:07:47

+0

你能帮我解决这个问题吗? – ManiMaranVasan 2014-11-03 11:12:03

回答

1

你需要做确保应用程序在使用模型联编程序将发布的值绑定到日期时间值时使用预期日期格式(绑定邮件时ED值到您的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(); 
+0

感谢您的回复,此代码工作正常,但是当我选择日期(2014年10月30日)时,它将该日期转换为(2014年1月30日)。您是否在当地检查了此场景 – ManiMaranVasan 2014-11-04 05:56:32

+0

您还需要确保在将文本框中的现有值呈现为“dd/mm/yyyy”时使用的格式。你可以像DisplayFormat(DataFormatString =“{0:dd/MM/yyyy}”,ApplyFormatInEditMode = true)]一样使用DisplayFormatAttribute,但只适用于'EditorFor'而不是'TextBoxFor'。您可以创建一个自定义帮助器,例如[this one](http://stackoverflow.com/a/22071121/1836935)或您自己的日期编辑器模板 – 2014-11-04 09:25:24

+0

如果您使用的是MVC 5.1+,则可以将DisplayFormat属性和改变你的代码来使用EditorFor,同时仍然使用'@ Html.EditorFor(model => model.Dob,new {htmlAttributes = new {placeholder =“DOB”,@readonly =“readonly”,@ class =“ReadOnly”}})' – 2014-11-04 09:28:08

相关问题