ASP.NET MVC为编辑和显示模式应用不同的DisplayFormat [英] ASP.NET MVC Apply different DisplayFormat's for Edit and Display modes

查看:566
本文介绍了ASP.NET MVC为编辑和显示模式应用不同的DisplayFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题



我正在浏览



但是,当一个编辑的视图时,此日期会使用以下内容:

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

它显示一个可爱的HTML5日期选择器,但没有填写值,因为日期的格式错误(错误信息指定值'11 / 01/1900 00:00:00'确实不符合所需的格式'yyyy-MM-dd'。
这是一个问题,因为如果用户想要编辑一个条目,控件的日期永远不会被设置,所以用户我不知道前一个/当前值是什么。





A中途解决方案



我通过阅读其他类似问题找到的解决方案是将模型中的DisplayFormat设置为yyyy-MM-dd并制作适用于编辑控件:

  [DataType(DataType.Date)] 
[DisplayFormat(DataFormatString ={0 :yyyy-MM-dd},ApplyFormatInEditMode = true)]
public DateTime ReleaseDate {get;组; }

这是有效的,因为它在编辑期间自动填充HTML5日期选择器的值(使用 @ Html.EditorFor())





但是当我正在显示时(使用`@ Html.DisplayFor()),我会遇到丑陋的ISO格式日期:





真实问题



我的真实问题是:我可以在模型中设置正常显示和编辑模式的不同显示格式吗?




  • I不想使用文本框来编辑日期 - 我想使用HTML5日期选择器;它比使用文本框好得多。

  • 我也不是真的想要更改显示日期的所有视图,直接从模型中获取属性并使用自定义格式对其进行格式化,这有点否定了DisplayFormat的要点属性。 (我想使用 @ Html.DisplayFor 进行显示,并使用 @ Html.EditorFor 进行编辑)



我希望一切都有意义 - 我是asp.net的新手,所以不完全熟悉这些控件和属性。



编辑



我想要实现的是这样的:

  [DataType(DataType.Date)] 
[DisplayFormat(DataFormatString ={0:yyyy-MM-dd},ApplyFormatInEditMode = true)] //使用此格式for edit controls
[DisplayFormat(DataFormatString ={0:dd-MMM-yyyy},ApplyFormatInEditMode = false)] //使用此格式显示控件
public DateTime ReleaseDate {get;组; }

但是一个属性不能有两个显示格式属性。

解决方案

使用方法TextBoxFor的格式参数并覆盖type属性:

  @ Html.TextBoxFor(m => m.ReleaseDate,{0:yyyy-MM-dd},htmlAttributes:new {@ type =date})


The Problem

I'm running through the tutorials for ASP.NET MVC 4 (but I think this would also apply to MVC 5), in which you create a website for a movie store.

The movie model has a ReleaseDate property of type DateTime. Generally this date is going to be viewed more than it is edited, so I applied the following attributes to render it nicely.

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}")]
public DateTime ReleaseDate { get; set; }

When a view that displays this date renders it using:

@Html.DisplayFor(model => model.ReleaseDate)

it's displayed nicely formatted, as you'd expect:

But, when a view that edits this date renders it using:

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

it displays a lovely HTML5 date picker, but doesn't fill in the value because the date's in the wrong format (error message is The specified value '11/01/1989 00:00:00' does not conform to the required format, 'yyyy-MM-dd'. This is a problem, because if a user wants to edit an entry, the date from the control never gets set, so the user won't know what the previous/current value is.

A Half-way Solution

A solution I've found from reading other similar questions is to set the DisplayFormat in the model to yyyy-MM-dd and make is applicable for edit controls:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime ReleaseDate { get; set; }

This works, in that it auto populates the value for the HTML5 date picker during editing (using @Html.EditorFor()):

But I'm stuck with ugly ISO format dates for when I'm displaying (using `@Html.DisplayFor()):

The Real Question

My real question is: can I have different display formats for normal display and editing mode, set in the model?

  • I don't want to use a textbox for editing the date - I want to use the HTML5 date picker; it's much nicer than using a textbox.
  • I also don't really want to change all the views that display the date to directly get the property from the model and format it with custom formatting, that kinda negates the point of the DisplayFormat attribute. (I want to use @Html.DisplayFor for displaying and @Html.EditorFor for editing)

I hope that all makes sense - I'm new to asp.net, so not entirely familiar with these controls and attributes.

Edit

What I'm trying to achieve is something like this:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] // use this format for edit controls
[DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}", ApplyFormatInEditMode = false)] // use this format for display controls
public DateTime ReleaseDate { get; set; }

but you can't have two display format attributes for one property.

解决方案

Use format parameter for method TextBoxFor and override the type attribute:

@Html.TextBoxFor(m => m.ReleaseDate, "{0:yyyy-MM-dd}", htmlAttributes: new { @type="date" })

这篇关于ASP.NET MVC为编辑和显示模式应用不同的DisplayFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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