HtmlHelper - Display HTML String

Previously in the last tutorials, we learned how to use HtmlHelper methods to generate various form input elements. In this tutorial, we will learn how to create Html string literal using HtmlHelper classes in the razor view of an ASP.NET MVC application.

To generate html strings, HtmlHelper class provides us with two extension methods:

  • Display()
  • DisplayFor().

To illustrate the use of these two above methods, we will use the following Activities model as shown in the code snippet below:

//Demo: Activity Model
public class Activity
    public int Id {get; set;}
    public string ActivityName {get; set;}
    public string Description {get; set;}
    public string Status {get; set;}


The Html.Display() is a loosely typed method provided by the HtmlHelper class which generates a string in razor view for the specified property of the model.

Display() method Signature: MvcHtmlString Display(string expression)

Display() method has many overloads, just like every other methods in HtmlHelper class.

Example: Html.Display() in Razor View
Html Result:
"Write C# Tutorials"


DisplayFor helper method is a strongly typed extension method. It generates an Html string for the model object property specified using a lambda expression.

DisplayFor() method Signature: MvcHtmlString DisplayFor(<Expression<Func<TModel,TValue>> expression)

Just like the Display method above, the DisplayFor method can also be method overloaded. The below code snippet illustrate the use of the DisplayFor() method.

//Example: DisplayFor() in the Razor 
@model Activity
@Html.DisplayFor(a => a.ActivityName)
Html Result:
" Write tutorials on C#"

In the above example, we have specified the ActivityName property of the Activity model using lambda expression in the DisplayFor() method. So, it generates an Html string with the value of ActivityName property, which is "Write tutorials on C#" as in the above example.

Inviting useful, relevant, well-written and unique guest posts