Access displayName attribute from the model in MVC View

前端 未结 8 710
不思量自难忘°
不思量自难忘° 2021-02-06 22:51

If my model have

[DisplayName(\"First Name\")]
public string firstName { get; set; }

Then I can print it in the View with LabelFor



        
8条回答
  •  误落风尘
    2021-02-06 23:24

    Building on Darren Oster's answer:

    @Html.DisplayNameFor(x => x.acc_first)
    

    Here's a link to the documentation for this: https://msdn.microsoft.com/en-us/library/system.web.mvc.html.displaynameextensions.displaynamefor(v=vs.118).aspx

    You could use this in your view code like this:

    @{ 
        var foo = Html.DisplayNameFor(x => x.acc_first);
    
        // call function
        DoStuff(foo);
    }
    

提交回复
热议问题