Assume you have an entity named Test with the First and Last properties:
public class Test {
public string First { get; set; }
public string Last { get; set; }
}
You can use DisplayFormat.DataFormatString and DisplayFormat.NullDisplayText to achieve your purpose:
public class Test {
[Display(Name = "First Name")]
[DisplayFormat(DataFormatString = "'{0}'", NullDisplayText = "'null'")]
public string First { get; set; }
[Display(Name = "Last Name")]
[DisplayFormat(DataFormatString = "'{0}'", NullDisplayText = "'null'")]
public string Last { get; set; }
}
AND in view:
@Html.DisplayFor(model => model.First)
@Html.DisplayFor(model => model.Last)
I change the answer too:
[DisplayFormat(DataFormatString = "'{0}'", NullDisplayText = "null")]