Guest User

Untitled

a guest
Aug 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. ASP.NET MVC Razor: Ternary
  2. @(string.IsNullOrEmpty(Model.Prop) ? "null" : "'" + Model.Prop + "'")
  3.  
  4. public class Test {
  5.  
  6. public string First { get; set; }
  7.  
  8. public string Last { get; set; }
  9. }
  10.  
  11. public class Test {
  12.  
  13. [Display(Name = "First Name")]
  14. [DisplayFormat(DataFormatString = "'{0}'", NullDisplayText = "'null'")]
  15. public string First { get; set; }
  16.  
  17. [Display(Name = "Last Name")]
  18. [DisplayFormat(DataFormatString = "'{0}'", NullDisplayText = "'null'")]
  19. public string Last { get; set; }
  20. }
  21.  
  22. @Html.DisplayFor(model => model.First)
  23.  
  24. @Html.DisplayFor(model => model.Last)
  25.  
  26. [DisplayFormat(DataFormatString = "'{0}'", NullDisplayText = "null")]
  27.  
  28. public class MyViewModel
  29. {
  30. [DisplayFormat(NullDisplayText = "null", DataFormatString = "'{0}'"]
  31. public string MyProperty { get; set; }
  32. }
  33.  
  34. @model MyViewModel
  35. ...
  36. @Html.DisplayFor(x => x.MyProperty)
Add Comment
Please, Sign In to add comment