0

I have a class like this:

Public Class AuthKey
    <StringLength(45, ErrorMessage:="Name must not less then 5 characters and not exceed 45 characters.", MinimumLength:=5)>
    <Display(Name:="Name", Description:="Authentication name.")> _
    Public Property name As String
End Class

I'd like to view the property display name and description, not the property value. I try to both @html.display("Name") or @html.display("Description") and @html.displayfor(function (m) m.name) all show nothing. How to show it?

Buzz
  • 321
  • 2
  • 3
  • 20
  • If your Model "Name" Property contain any value then Display/DisplayFor will display text – 111 Mar 16 '15 at 05:32
  • No, I would like to show the "Name" and or "Authentication name." value. Just like if i use html.labelfor, it will show the label name like this: . I just want the plain text of Name, without any html tag. – Buzz Mar 16 '15 at 05:42
  • you want to add model class in view when you create view page then try – Kishan Mar 16 '15 at 05:49
  • 1
    Use `DisplayNameFor()` –  Mar 16 '15 at 05:54
  • @StephenMuecke, excellent, thanks! It shows what I need. DisplayNameFor show the name field. But I have another field which is "Description". How to show it? – Buzz Mar 16 '15 at 06:03
  • 1
    @BennyChen, Its not used directly by any of the built in helpers, but you can create you own or access from `ViewData.ModelMetadata`. The [answers here](http://stackoverflow.com/questions/6578495/how-do-i-display-the-displayattribute-description-attribute-value) show a few options –  Mar 16 '15 at 06:13
  • 1
    @StephenMuecke, thanks for the links. It works! I ended up by access the metadata directly instead creating new custom helper. So here it is: @ModelMetadata.FromLambdaExpression(Of AuthKey, String)(Function(model) model.name, ViewData).Description – Buzz Mar 16 '15 at 06:37
  • you don't ask about description in question – Kishan Mar 16 '15 at 06:45

1 Answers1

1

add model class in your view page after that try like this

 @Html.DisplayNameFor(model => model.name)

how-to-access-the-model-from-the-view

Community
  • 1
  • 1
Kishan
  • 1,182
  • 12
  • 26
  • This will display the name, but not the description. @stephenmuecke method works. I need to create custom helper or access the metadata directly. – Buzz Mar 16 '15 at 06:41
  • @BennyChen, I only commented. This answer is correct based on you original question and you should mark it as accepted –  Mar 16 '15 at 07:05