"asp格式"不适用于标签助手 [英] "asp-format" not applied to tag helpers

查看:53
本文介绍了"asp格式"不适用于标签助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的mvc 6项目中,将"asp-format"标记与taghelper元素一起使用时遇到问题.

I'm facing a problem using "asp-format" tag with taghelper element in my mvc 6 project.

想法是通过以下方式格式化日期输入元素:

The idea is to format a date input element this way:

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

此"StartDate"属性在我的模型中,以这种方式声明:

This "StartDate" property is in my model, declared this way:

public DateTime StartDate {get; set; }

出于一个奇怪的原因,该元素从未格式化过,并且总是这样显示:

For a strange reason, this element is never formatted, and is presented always like this:

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

所以我创建了一个viewmodel类,并定义了一个属性来容纳整个人模型.

So I created a viewmodel class and defined a property to hold the entire person model.

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

然后在视图中使用此类,格式化就可以完成.

And using this class in the view, the formatting works.

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

---> 29/02/2016

推荐答案

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

You can provide the format in model itself like

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

  public DateTime StartDate {get; set; }

在您看来就像

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

2)您也可以在模型类中不提供日期格式的情况下这样做

2) You can also do this without providing date format in model class

@Html.TextBoxFor(m => m.StartDate, "{0:dd/MM/yyyy}")

这篇关于"asp格式"不适用于标签助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆