26

I have a class

public class Item
{
    public int ItemId { get; set; }

    [Required(ErrorMessage = "Category is required")]
    [Range(1, int.MaxValue, ErrorMessage = "Category is required")]
    public int CategoryId { get; set; }

    [Display(Name = "Current password")]
    [Required(ErrorMessage = "Name is required")]
    [StringLength(160)]
    public string Name { get; set; }

    [Required(ErrorMessage = "Price is required")]
    [Range(0.01, 100.00,
        ErrorMessage = "Price must be between 0.01 and 100.00")]
    public decimal Price { get; set; }

    public virtual Category Category { get; set; }
}

In my controller I pass an instance of this to view

public ActionResult Index()
    {
        var model = new Item
        {
            CategoryId = 1,
            Name = "aaa",
            Price = 2
        };

        return View("Index", model);            
    }

then in view I try to display name using

@model GenericShop.Models.Item

<p>                               
 @Html.DisplayNameFor(m => m.Name)                                       
</p>

and get the following error

Compiler Error Message: CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'DisplayNameFor' and no extension method 'DisplayNameFor' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

@Html.DisplayFor(m => m.Name) works fine, but I just cant see why

@Html.DisplayNameFor(m => m.Name) does not.

DisplayFor displays the value for the model item and DisplayNameFor simply displays the name of the property?

Minsk
  • 315
  • 1
  • 3
  • 9

1 Answers1

41

Almost there. :)

The DisplayNameFor shows the name of the property or the string defined in the display attribute for the property.

public class Item
{
    public int ItemId { get; set; }

    [Display(Name = "Current name")]
    [Required(ErrorMessage = "Name is required")]
    [StringLength(160)]
    public string Name { get; set; }

    [Required(ErrorMessage = "Price is required")]
    [Range(0.01, 100.00,
        ErrorMessage = "Price must be between 0.01 and 100.00")]
    public decimal Price { get; set; }

}

Then @Html.DisplayNameFor(m => m.Name) would show 'Current name'.

@Html.DisplayNameFor(m => m.Price) would just show Price.

Note that you can also localize the display attribute like this:

[Display(ResourceType = typeof(MyResources), Name = "Name")]
public string Name{ get; set; }

Which in turn will look in the MyResources resc file. (If setup is done correctly).

The Html.DisplayFor shows the value of the field.

Kat
  • 239
  • 2
  • 12
RoteS
  • 1,405
  • 13
  • 10
  • In your answer, where you meant if you would... You use DisplayNameFor in both cases and this is confusing. – Minsk Feb 13 '16 at 21:02
  • 1
    The first Time I use @Html.DisplayNameFor(m => m.Name) which links to the 8th line in the code example. If you look at line 5 you see [display(name= "current name")] this will cause the html to render "current name" In the @Html.DisplayNameFor(m => m.Price) I link to the line public decimal Price { get; set; } this property has no [display...] above it so asp.net mvc renders just the name of the propery (which is Price) Is this clearer? – RoteS Feb 13 '16 at 21:05
  • 4
    yes, yes. So the `DisplayNameFor(model => model.Field1)` takes the Display(name="abcd") and prints abcd. If there is no display name, he will use the property name instead. – Minsk Feb 13 '16 at 21:10
  • 3
    Thank you, now it is clear. And DisplayFor displays the actual value of the model item property? – Minsk Feb 13 '16 at 21:12
  • 1
    Yes it displays the value as text. If you want to be able to edit it you need to use something like Html.EditorFor(c => c.propertyname) – RoteS Feb 13 '16 at 21:13
  • Rotes, please check my other question, upvote = 5 points and I will be able to upvote you. – Minsk Feb 13 '16 at 21:14