I am new to MVC and know how to use Html.Displayfor(), but I don’t know when to use it?
Any idea?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
DisplayForhelper renders the corresponding display template for the given type. For example, you should use it with collection properties or if you wanted to somehow personalize this template. When used with a collection property, the corresponding template will automatically be rendered for each element of the collection.Here’s how it works:
will render the default template for the given type. For example, if you have decorated your view model property with some formatting options, it will respect those options:
In your view, when you use the
DisplayForhelper, it will render the property by taking into account this format whereas if you used simply@Model.SomeProperty, it wouldn’t respect this custom format.Always use it when you want to display a value from your view model. Always use:
instead of: