I can't get @Html to work with MVC 3. To render a formatted string, use DisplayFor.

asp.net-mvc c# data-annotations entity-framework linq-to-entities

Question

Although I've done a lot of Googling, I haven't been able to figure this one out. I'm thinking it's quite straightforward.

I'm utilizing code-first Entity Framework on an MVC 3 shopping cart website that I'm developing. I'm providing you a model that consists of a list of Product objects, each of which has the following 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; }

Since item is the product object in the loop, I was hoping that the DisplayFormat property would enable the following line in the view to format the data as a currency:

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

However, this completely disregards the formatting. The only method I've found to make it work so far is to use this instead:

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

But I would prefer use @Html if it were feasible. Due to the way DisplayFor is built, it can deal with nulls. I'm certain that it will be 0 or higher. In reality, with the validation, there will always be some amount; nevertheless, before moving on, I want to be sure that there isn't a more accurate method to do this.

Any information would be much welcomed on this!

UPDATE

Darin responded and noted that it did indeed function, which prompted me to review what I had really sent to the view. Despite having a class named ProductModel with the DisplayFormat feature, I discovered that I was really returning another model that contained a list of items. It produced a list of the Product data class, not the ProductModel class, as I later discovered. This is called the ProductListModel.

Thus, it turned out to be rather easy in the end. I just regret wasting a half-evening on it. Darin, I appreciate you motivating me to double-check everything.

1
12
9/6/2011 5:52:06 AM

Accepted Answer

But this doesn't apply the formatting at all.

Oh, you've got to be really doing something wrong since the DisplayFormat property ought to function. For instance:

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 use this program, I receive exactly what I was expecting:$0.56 .

What gives, then? How is your situation different from mine?

16
9/5/2011 8:57:59 PM

Popular Answer

One possibility is to design aCurrency.cshtmlDisplayTemplate and take away theDisplayFormat attribute. Next, in yourCurrency.cshtml It might be formatted as you like.

In the process of templating,DataType is used to choose a template.

Darin pointed out that this should still function without doing so. If so, do you already have one?Currency.cshtml record in yourDisplayTemplates that disregards formatting?



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow