2017-07-06 85 views

回答

0

在計算器在這裏讀了很多的答案後,我來到了這個解決方案:

在我介紹一個新的,沒有對應的屬性模型:

... 
    [NotMapped] 
    [DataType(DataType.Date)] 
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
    public DateTime GeburtsdatumChromeEdit 
    { 
     get { return this.Geburtsdatum; } 
     set { this.Geburtsdatum = value; } 
    } 
... 

這是必須添加的dataannotiation DisplayFormat與Chrome想要的格式。它將值傳遞給原始屬性。

在視圖我一倍的日期輸入和封裝兩組由div的特殊類:

<div class="date4Normal"> 
     @*Tut nicht in Chrome, da dieser dann einen Input vom Type "Date" verwendet und dieser verlangt dann ein Normdatum yyyy-mm-dd*@ 
     @Html.EditorFor(model => model.Geburtsdatum) 
     @Html.ValidationMessageFor(m => m.Geburtsdatum, String.Empty, new {@class = "text-danger"}) 
    </div> 
    <div class="date4Chrome"> 
     @Html.EditorFor(model => model.GeburtsdatumChromeEdit) 
     @Html.ValidationMessageFor(m => m.GeburtsdatumChromeEdit, String.Empty, new { @class = "text-danger" }) 
     @*@Html.TextBoxFor(model => model.Geburtsdatum, "{0:dd.MM.yyyy}") 
     @Html.ValidationMessageFor(m => m.Geburtsdatum, String.Empty, new { @class = "text-danger" })*@ 
    </div> 

與函數我在計算器發現(How can I tell if a browser supports <input type='date'>)如果實際的瀏覽器使用我能察覺日期輸入:

隨着未來功能我刪除輸入塊我不需要:

function switchForChrome() { 
     var isChrome = checkDateInput(); 
     if (isChrome) { 
      $('.date4Normal').remove(); 
     } else { 
      $('.date4Chrome').remove(); 
     } 
    }; 

$(document).ready函數中我稱之爲switchForChrome();

在這一點上它似乎工作,但日期的驗證總是失敗。

所以我添加一些代碼來解決(ASP.NET MVC 4 avoid generation of data-val-date for datetime):

if (checkDateInput()) { 
    $.validator.addMethod('date', 
     function (value, element) { 
      return true; 
    }); 
} 

最後它的工作原理。 爲了顯示我用原來的屬性(Geburtsdatum),所以我得到的特定語言的格式和編輯要麼是原始屬性(IE,火狐)或新的屬性GeburtsdatumChromeEdit(鉻,歌劇)。

0

以前的答案可以幫助,但是當你把它放在服務器,如果你像我一樣做的過程與日期字段的信息累計就會死機,因此,解決這個問題的正確方式和沒有前途的問題是由@RAVI VAGHELA上this後給出

您需要修改的Global.asax,並添加一個配置,具有記住,這將改寫所有的日期格式,並會不顧一切,你必須在服務器上,請小心,

protected void Application_BeginRequest(object sender, EventArgs e) 
     { 
      var newCulture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone(); 
      newCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy"; 
      newCulture.DateTimeFormat.DateSeparator = "-"; 
      Thread.CurrentThread.CurrentCulture = newCulture; 
     } 

注意:這種方法已經測試在蔚藍的,(在英語的Windows Server 2012)(西部和歐洲)本地服務器和本地(IIS表示在Windows 10),請告知,如果有一個關於其他語言此方法或區域

+0

對不起問題,但我不明白,你的意思是「你使用日期字段的信息進行計算」 - 什麼崩潰? – PBum

+0

例如,當您需要基於選定的日期和用戶給出的某些參數計算孩子的FPP(可出生日期)時 – sGermosen

相關問題