在MVC 3,我不能让@ Html.DisplayFor渲染格式化字符串 [英] In MVC 3, I can't get @Html.DisplayFor to render a formatted string

查看:1095
本文介绍了在MVC 3,我不能让@ Html.DisplayFor渲染格式化字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这是一个相当简单的,虽然很多谷歌搜索后,我没有去过能够解决它。

我工作的一个购物车网站与MVC 3,用code-第一实体框架。我送过该模型是产品对象的列表,每个这些对象包括此属性:

  [必需的(=的ErrorMessage这是一个必填字段。)]
[数据类型(DataType.Currency)
[范围(1.00,500.00,的ErrorMessage =的产品不能是免费的。)
[DisplayFormat(DataFormatString ={0:C})]
[显示名称(价格)]
公共双价{搞定;组; }

我希望在DisplayFormat属性将导致以下行视图格式化值作为货币(项目是在循环中的产品对象):

  @ Html.DisplayFor(modelItem => item.Price)

但这并不在所有应用格式。到目前为止,我已经能够得到它的工作的唯一办法就是用这个代替:

  @的String.Format({0:C},item.Price)

但是,如果可能的话,我宁愿使用@ Html.DisplayFor,因为它是专门用来处理之类的东西空值。我知道这将是0以上。事实上与验证它永远是一定量的 - 但我要确保没有这样做之前,我进行了比较正确的做法

这方面的消息将是最AP preciated!

更新

达林回答,并指出它的工作,这引起我回去过了我的实际发送到该视图。我意识到,尽管我有一个叫产品型号类,它具有DisplayFormat属性,我其实是回归,其中包含的产品列表另一种模式。这就是所谓的ProductListModel,我意识到,它返回的产品数据类的列表 - !不是产品型号类

那么到底它实际上是非常简单的。只是希望我没有浪费了一半的夜晚。谢谢你鼓励我回去查得当,达林!


解决方案

  

但是,这并不在所有适用的格式。


呵呵,你必须做一些非常错误的DisplayFormat属性应该工作。例如:

型号:

 公共类MyViewModel
{
    [必需的(=的ErrorMessage这是一个必填字段。)]
    [数据类型(DataType.Currency)
    [范围(1.00,500.00,的ErrorMessage =的产品不能是免费的。)
    [DisplayFormat(DataFormatString ={0:C})]
    [显示名称(价格)]
    公共双价{搞定;组; }
}

控制器:

 公共类HomeController的:控制器
{
    公众的ActionResult指数()
    {
        VAR模型=新MyViewModel
        {
            价格= 0.56
        };
        返回查看(模型);
    }
}

查看(〜/查看/主页/ Index.cshtml

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

当我运行该应用程序我得到的,因为完全预期, $ 0.56

那么怎么办?请问你的情况比我的不同?

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!

解决方案

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?

这篇关于在MVC 3,我不能让@ Html.DisplayFor渲染格式化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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