在 MVC 3 中,我无法让 @Html.DisplayFor 呈现格式化字符串

发布于 2024-12-03 13:47:25 字数 1035 浏览 2 评论 0原文

我希望这是一个非常简单的问题,尽管经过大量谷歌搜索后,我还没有能够解决这个问题。

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风无影 2024-12-10 13:47:25

但这根本不应用格式。

哦,你一定做了一些非常错误的事情,因为 DisplayFormat 属性应该起作用。例如:

模型:

public class MyViewModel
{
    [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; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Price = 0.56
        };
        return View(model);
    }
}

视图(~/Views/Home/Index.cshtml):

@model MyViewModel
@Html.DisplayFor(x => x.Price)

当我运行此应用程序时,正如完全预期的那样,我得到$0.56

那么什么给出呢?你的场景和我的有什么不同?

But this doesn't apply the formatting at all.

Oh, you gotta be doing something very wrong as the DisplayFormat attribute should work. For example:

Model:

public class MyViewModel
{
    [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; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Price = 0.56
        };
        return View(model);
    }
}

View (~/Views/Home/Index.cshtml):

@model MyViewModel
@Html.DisplayFor(x => x.Price)

When I run this application I get, as totally expected, $0.56.

So what gives? How does your scenario differs than mine?

假扮的天使 2024-12-10 13:47:25

一种选择是创建 Currency.cshtml DisplayTemplate 并删除 DisplayFormat 属性。然后在您的 Currency.cshtml 中,您可以按照您喜欢的方式对其进行格式化。

作为模板化过程的一部分,DataType 用于选择模板。

然而,正如达林所说,即使不这样做也应该可以工作。您的 DisplayTemplates 中是否已经有一个不应用格式设置的 Currency.cshtml 文件?

One option is to create a Currency.cshtml DisplayTemplate and remove the DisplayFormat attribute. Then in your Currency.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 your DisplayTemplates that does not apply formatting?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文