在 MVC 3 中,我无法让 @Html.DisplayFor 呈现格式化字符串
我希望这是一个非常简单的问题,尽管经过大量谷歌搜索后,我还没有能够解决这个问题。
我正在使用 MVC 3 开发一个购物车网站,并使用代码优先的实体框架。我发送的模型是 Product 对象的列表,每个对象都包含此属性:
[Required(ErrorMessage = "This is a required field.")]
[DataType(DataType.Currency)]
[Range(1.00, 500.00, ErrorMessage = "Products can not be free.")]
[DisplayFormat(DataFormatString = "{0:C}")]
[DisplayName("Price")]
public double Price { get; set; }
我希望 DisplayFormat 属性会导致视图中的以下行将值格式化为货币(项目是产品)循环中的对象):
@Html.DisplayFor(modelItem => item.Price)
但这根本不应用格式。到目前为止,我能够让它工作的唯一方法是使用它:
@String.Format("{0:C}", item.Price)
但如果可能的话,我宁愿使用@Html.DisplayFor,因为它旨在处理空值之类的事情。我知道它会是 0 或更多。事实上,通过验证,它总是会是一定数量的——但我想在继续之前确保没有更正确的方法来做到这一点。
任何有关这方面的信息将不胜感激!
更新
达林回答并指出它确实有效,这使我回顾了我实际发送到视图的内容。我意识到,虽然我有一个名为 ProductModel 的类,它具有 DisplayFormat 属性,但我实际上返回了另一个包含产品列表的模型。这称为 ProductListModel,我意识到它返回了 Product 数据类的列表 - 而不是 ProductModel 类!
所以最后其实很简单。只希望我没有在上面浪费半个晚上。谢谢你激励我回去好好检查,达林!
I'm hoping this is quite a simple one, although after lots of Googling, I've not been able to work it out.
I'm working on a shopping cart site with MVC 3, and using code-first Entity Framework. The model I'm sending over is a list of Product objects, and each of those objects includes this property:
[Required(ErrorMessage = "This is a required field.")]
[DataType(DataType.Currency)]
[Range(1.00, 500.00, ErrorMessage = "Products can not be free.")]
[DisplayFormat(DataFormatString = "{0:C}")]
[DisplayName("Price")]
public double Price { get; set; }
I was hoping that the DisplayFormat attribute would cause the following line in the view to format the value as a currency (item is the product object in the loop):
@Html.DisplayFor(modelItem => item.Price)
But this doesn't apply the formatting at all. So far the only way I've been able to get it to work is to use this instead:
@String.Format("{0:C}", item.Price)
But if it's possible, I'd rather use @Html.DisplayFor as it's designed to handle things like nulls. I know it's going to be 0 or more. In fact with the validation it'll always be some amount - but I want to make sure there isn't a more correct way of doing this before I carry on.
Any information on this would be most appreciated!
UPDATE
Darin answered and pointed out that it does work, which caused me to go back over what I was actually sending over to the view. I realised that although I have a class called ProductModel, which has the DisplayFormat attribute, I was actually returning another model which contains a list of products. This is called ProductListModel and I realised that it returned a list of the Product data class - not the ProductModel class!
So in the end it was actually very simple. Just wish I hadn't wasted half an evening on it. Thanks for inspiring me to go back and check properly, Darin!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哦,你一定做了一些非常错误的事情,因为 DisplayFormat 属性应该起作用。例如:
模型:
控制器:
视图(
~/Views/Home/Index.cshtml
):当我运行此应用程序时,正如完全预期的那样,我得到
$0.56
。那么什么给出呢?你的场景和我的有什么不同?
Oh, you gotta be doing something very wrong as the DisplayFormat attribute should work. For example:
Model:
Controller:
View (
~/Views/Home/Index.cshtml
):When I run this application I get, as totally expected,
$0.56
.So what gives? How does your scenario differs than mine?
一种选择是创建
Currency.cshtml
DisplayTemplate
并删除DisplayFormat
属性。然后在您的Currency.cshtml
中,您可以按照您喜欢的方式对其进行格式化。作为模板化过程的一部分,
DataType
用于选择模板。然而,正如达林所说,即使不这样做也应该可以工作。您的
DisplayTemplates
中是否已经有一个不应用格式设置的Currency.cshtml
文件?One option is to create a
Currency.cshtml
DisplayTemplate
and remove theDisplayFormat
attribute. Then in yourCurrency.cshtml
you would format it however you like.As part of the templating process,
DataType
is used to select a template.However, as Darin says, this should work without doing this. Do you, by any chance already have a
Currency.cshtml
file in yourDisplayTemplates
that does not apply formatting?