Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access displayName attribute from the model in MVC View

If my model have

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

Then I can print it in the View with LabelFor

@Html.LabelFor(model => model.acc_first) 

It will then render as

<label for="firstName">First Name</label> 
  • But how can I "raw" read the property (FirstName) attributes in the View? If for example, I want to send the value to a function on the View page
like image 793
Joakim Avatar asked Sep 14 '11 15:09

Joakim


People also ask

How do you pass value from view to model?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

How do you specify a model in view?

To declare the model type, use the @model directive. Show activity on this post. Note the lowercase @model since uppercase prints the value of the Model property.

What is view in MVC model?

ViewModel = Model that is created to serve the view. ASP.NET MVC view can't have more than one model so if we need to display properties from more than one model in the view, it is not possible. ViewModel serves this purpose. View Model is a model class that can hold only those properties that are required for a view.


1 Answers

@Html.DisplayNameFor(x => x.acc_first) 
like image 126
Darren Oster Avatar answered Oct 02 '22 02:10

Darren Oster