2014-11-03 123 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