How to use DisplayNameForModel?

I tried using this as the title for the view, but it returns an empty string. In the Razor layout, I have something like:

@model IEnumerable<MVCApp.Models.Model>
<h2>@Html.DisplayNameForModel()</h2>

Do I need to set something in the model definition itself? I tried data annotation [Display(Name="Model Name")], but this is a build error:

The display attribute is not valid for this ad type. It is valid only for declarations of 'method, property, indexer, field, param'.

The documentation for DisplayNameExtensions.DisplayNameForModel Method is concise. The syntax calls the parameter, but says:

No overload for 'DisplayNameForModel' method takes 1 argument

The Usage section says: "When you use the instance method syntax to call this method, omit the first parameter"

So how can I use this method to return something?

+4
source share
2 answers

I just used the default MVC 5 project in VS2013 and it @Html.DisplayNameForModel()works without problems.

First, you use incorrect data annotation in your presentation model. You want to use [DisplayName("My Model Name")], not[Display()]

[DisplayName("Test View Model")]
public class TestViewModel

{
    public string TestProperty { get; set; }
}

-, html, MSDN, Html- MVC. , . , , , , , .

<h2>@Html.DisplayNameForModel()</h2>

, html. * .

enter image description here

+5

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

-3

All Articles