Typ danych MVC4.Data EditorFor nie wyświetla wartości daty w Chrome, dobrze w Internet Explorer
Używam typu danych.Atrybut daty na moim modelu i edytor wg mnie. To działa dobrze w Internet Explorer 8 i Internet Explorer 9 , ale w Google Chrome pokazuje selektor daty i zamiast wyświetlać wartość wyświetla po prostu "Miesiąc/Dzień/Rok" w wyblakłym szarym tekście.
Dlaczego Google Chrome nie wyświetla wartości?
Model:
[DataType(DataType.Date)]
public Nullable<System.DateTime> EstPurchaseDate { get; set; }
Widok:
<td class="fieldLabel">Est. Pur. Date</td>
<td class="field">@Html.EditorFor(m=>m.EstPurchaseDate)</td>
8 answers
Gdy dekorujesz właściwość modelu [DataType(DataType.Date)]
domyślnym szablonem w ASP.NET MVC 4 generuje pole wejściowe type="date"
:
<input class="text-box single-line"
data-val="true"
data-val-date="The field EstPurchaseDate must be a date."
id="EstPurchaseDate"
name="EstPurchaseDate"
type="date" value="9/28/2012" />
Przeglądarki obsługujące HTML5, takie jak Google Chrome, renderują to pole wejściowe za pomocą selektora daty.
Aby poprawnie wyświetlić datę, wartość musi być sformatowana jako 2012-09-28
. Cytat ze specyfikacji :
Value: a valid full-date as defined in [RFC 3339], with the additional Kwalifikacje, że rok składowa to cztery lub więcej cyfr reprezentująca liczbę większą niż 0.
Możesz wymusić ten format używając atrybutu DisplayFormat
:
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> EstPurchaseDate { get; set; }
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-27 08:19:38
W MVC5. 2 Dodaj datę.cshtml do folderu ~ / Views / Shared/EditorTemplates:
@model DateTime?
@{
IDictionary<string, object> htmlAttributes;
object objAttributes;
if (ViewData.TryGetValue("htmlAttributes", out objAttributes))
{
htmlAttributes = objAttributes as IDictionary<string, object> ?? HtmlHelper.AnonymousObjectToHtmlAttributes(objAttributes);
}
else
{
htmlAttributes = new RouteValueDictionary();
}
htmlAttributes.Add("type", "date");
String format = (Request.UserAgent != null && Request.UserAgent.Contains("Chrome")) ? "{0:yyyy-MM-dd}" : "{0:d}";
@Html.TextBox("", Model, format, htmlAttributes)
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-13 22:00:15
Jako dodatek do odpowiedzi Darina Dimitrowa:
Jeśli chcesz, aby ta konkretna linia używała tylko określonego (innego niż standardowy) formatu, możesz użyć w MVC5:
@Html.EditorFor(model => model.Property, new {htmlAttributes = new {@Value = @Model.Property.ToString("yyyy-MM-dd"), @class = "customclass" } })
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-19 12:47:51
W MVC 3 musiałem dodać:
using System.ComponentModel.DataAnnotations;
Wśród zastosowań przy dodawaniu właściwości:
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
Szczególnie jeśli dodajesz te właściwości w .plik edmx jak ja. Znalazłem to domyślnie .pliki edmx nie mają tego za pomocą, więc dodanie tylko propeties nie wystarczy.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-03 11:56:17
Jeśli usuniesz [DataType(DataType.Date)]
ze swojego modelu, pole wprowadzania w Chrome będzie renderowane jako type="datetime"
i nie będzie wyświetlać datepickera.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-28 10:50:21
Nadal miałem problem z przekazaniem formatu RRRR-MM-dd, ale obejrzałem go zmieniając datę.cshtml:
@model DateTime?
@{
string date = string.Empty;
if (Model != null)
{
date = string.Format("{0}-{1}-{2}", Model.Value.Year, Model.Value.Month, Model.Value.Day);
}
@Html.TextBox(string.Empty, date, new { @class = "datefield", type = "date" })
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-11 13:43:29
Odpowiedź na typ danych MVC4.Data EditorFor nie wyświetla wartości daty w Chrome, dobrze w IE
We wzorze musisz mieć następujący typ deklaracji:
[DataType(DataType.Date)]
public DateTime? DateXYZ { get; set; }
Lub
[DataType(DataType.Date)]
public Nullable<System.DateTime> DateXYZ { get; set; }
Nie musisz używać następującego atrybutu:
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
W dniu.cshtml użyj tego szablonu:
@model Nullable<DateTime>
@using System.Globalization;
@{
DateTime dt = DateTime.Now;
if (Model != null)
{
dt = (System.DateTime)Model;
}
if (Request.Browser.Type.ToUpper().Contains("IE") || Request.Browser.Type.Contains("InternetExplorer"))
{
@Html.TextBox("", String.Format("{0:d}", dt.ToShortDateString()), new { @class = "datefield", type = "date" })
}
else
{
//Tested in chrome
DateTimeFormatInfo dtfi = CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat;
dtfi.DateSeparator = "-";
dtfi.ShortDatePattern = @"yyyy/MM/dd";
@Html.TextBox("", String.Format("{0:d}", dt.ToString("d", dtfi)), new { @class = "datefield", type = "date" })
}
}
Baw się dobrze!
Pozdrawiam,
BlertonWarning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-31 13:32:04
Jeśli chcesz mieć kontrolę nad formatem daty (innymi słowy nie tylko format RRRR-mm-dd jest akceptowalny), innym rozwiązaniem może być dodanie właściwości pomocniczej, która jest typu string i dodanie walidatora daty do tej właściwości i bind do tej właściwości w interfejsie użytkownika.
[Display(Name = "Due date")]
[Required]
[AllowHtml]
[DateValidation]
public string DueDateString { get; set; }
public DateTime? DueDate
{
get
{
return string.IsNullOrEmpty(DueDateString) ? (DateTime?)null : DateTime.Parse(DueDateString);
}
set
{
DueDateString = value == null ? null : value.Value.ToString("d");
}
}
A oto walidator daty:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
public class DateValidationAttribute : ValidationAttribute
{
public DateValidationAttribute()
{
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value != null)
{
DateTime date;
if (value is string)
{
if (!DateTime.TryParse((string)value, out date))
{
return new ValidationResult(validationContext.DisplayName + " must be a valid date.");
}
}
else
date = (DateTime)value;
if (date < new DateTime(1900, 1, 1) || date > new DateTime(3000, 12, 31))
{
return new ValidationResult(validationContext.DisplayName + " must be a valid date.");
}
}
return null;
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-12 13:59:55