“asp-format”未应用于标记助手

时间:2016-02-29 13:11:52

标签: c# asp.net asp.net-mvc datetime-format

我在mvc 6项目中使用带有taghelper元素的“asp-format”标签时遇到问题。

我们的想法是以这种方式格式化日期输入元素:

<input asp-for="StartDate" asp-format="{0:dd/MM/yyyy}" />

这个“StartDate”属性在我的模型中,以这种方式声明:

public DateTime StartDate {get; set; }

由于一个奇怪的原因,这个元素永远不会被格式化,并且始终如下所示:

---> 02/29/2016 00:00:00

所以我创建了一个viewmodel类并定义了一个属性来保存整个人模型。

public class PersonViewModel 
{
    public Person Johndoe {get; set; }
}

在视图中使用此类,格式化工作。

<input asp-for="Johndoe.StartDate" asp-format="{0:dd/MM/yyyy}" />

---> 29/02/2016

3 个答案:

答案 0 :(得分:6)

您可以在模型中提供格式,如

 [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]

  public DateTime StartDate {get; set; }

在你看来就像

@Html.EditorFor(model=>model.StartTime)

2)您也可以在不提供模型类

的日期格式的情况下执行此操作
@Html.TextBoxFor(m => m.StartDate, "{0:dd/MM/yyyy}")

答案 1 :(得分:1)

添加类型=将格式化日期的日期

<input asp-for="StartDate" type="date" class="form-control" />

答案 2 :(得分:0)

我不得不使用

# empty list
listed <- list()
# fill the list with the plots
for (i in c(2:3)){
     listed[[i-1]] <- df[,-i]  %>%
                      gather(variable, value, -c(Period)) %>% 
                      ggplot(aes(Period, value)) + geom_line()
                 }

# to get the plots
listed[[1]]
listed[[2]]

[DataType(DataType.Date)]

在我的视图模型中。

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]

我的视图如下:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
[DataType(DataType.Date)]
public DateTime From { get; set; }
相关问题