Предположим, у вас есть объект с именем Test
со свойствами First
и Last
:
public class Test {
public string First { get; set; }
public string Last { get; set; }
}
Вы можете использовать DisplayFormat.DataFormatString
и DisplayFormat.NullDisplayText
для достижения своей цели:
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; }
}
И в поле зрения:
@Html.DisplayFor(model => model.First)
@Html.DisplayFor(model => model.Last)
Я тоже меняю ответ:
[DisplayFormat(DataFormatString = "'{0}'", NullDisplayText = "null")]