DisplayFor<TModel,TValue>(HtmlHelper<TModel>, Expression<Func<TModel,TValue>>, String, String, Object) Returns HTML markup for each property in the object that is represented by the specified expression, using the template, an HTML field ID, and additional view data.
DisplayNameFor(m => m.Name) does not. DisplayFor displays the value for the model item and DisplayNameFor simply displays the name of the property?
The Html. Raw Helper Method is used to display HTML in Raw format i.e. without encoding in ASP.Net MVC Razor. Configuring Bundles. Please refer the following article for complete information on how to configure Bundles in ASP.Net MVC project. Using Bundles (ScriptBundle) in ASP.Net MVC Razor.
Creates an HTML-encoded string using the specified text value. IsNullOrEmpty(MvcHtmlString) Determines whether the specified string contains content or is either null or empty.
Html.DisplayFor()
will render the DisplayTemplate that matches the property's type.
If it can't find any, I suppose it invokes .ToString()
.
If you don't know about display templates, they're partial views that can be put in a DisplayTemplates
folder inside the view folder associated to a controller.
Example:
If you create a view named String.cshtml
inside the DisplayTemplates
folder of your views folder (e.g Home
, or Shared
) with the following code:
@model string
@if (string.IsNullOrEmpty(Model)) {
<strong>Null string</strong>
}
else {
@Model
}
Then @Html.DisplayFor(model => model.Title)
(assuming that Title
is a string) will use the template and display <strong>Null string</strong>
if the string is null, or empty.
I think the main benefit would be when you define your own Display Templates, or use Data annotations.
So for example if your title was a date, you could define
[DisplayFormat(DataFormatString = "{0:d}")]
and then on every page it would display the value in a consistent manner. Otherwise you may have to customise the display on multiple pages. So it does not help much for plain strings, but it does help for currencies, dates, emails, urls, etc.
For example instead of an email address being a plain string it could show up as a link:
<a href="mailto:@ViewData.Model">@ViewData.TemplateInfo.FormattedModelValue</a>
DisplayFor
is also useful for templating. You could write a template for your Model, and do something like this:
@Html.DisplayFor(m => m)
Similar to @Html.EditorFor(m => m)
. It's useful for the DRY principal so that you don't have to write the same display logic over and over for the same Model.
Take a look at this blog on MVC2 templates. It's still very applicable to MVC3:
http://www.dalsoft.co.uk/blog/index.php/2010/04/26/mvc-2-templates/
It's also useful if your Model has a Data annotation. For instance, if the property on the model is decorated with the EmailAddress
data annotation, DisplayFor
will render it as a mailto:
link.
In general if it is used for just one property it appears that the generated HTML is the same.
For example this code
<td>@Html.DisplayFor(modelItem=>item.Genre.Name)</td>
<td>@item.Genre.Name, This is direct from Item</td>
generates this HTML
<td>myClassNameProperty</td>
<td>myClassNameProperty, This is direct from Item</td>
At the same time now if i want to display all properties in one statement for my class "Genre" in this case, i can use @Html.DisplayFor() to save on my typing, for least
i can write @Html.DisplayFor(modelItem=>item.Genre) in place of writing a separate statement for each property of Genre as below
@item.Genre.Name
@item.Genre.Id
@item.Genre.Description
and so on depending on number of properties.
From html-helpers documentation:
An HTML Helper is just a method that returns a string. The string can represent any type of content that you want. For example, you can use HTML Helpers to render standard HTML tags like HTML and tags. You also can use HTML Helpers to render more complex content such as a tab strip or an HTML table of database data.
To render more complex html mvc custom html templates or custom tag helpers in asp.net core are used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With