Guest User

Untitled

a guest
Jun 26th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. <ul>
  2. @foreach (var mi in Model.MenuItems) {
  3. <li@(mi.Selected?" class="selected"":null)>
  4. @if (string.IsNullOrEmpty(mi.Title)) {
  5. <a href="@mi.Href">@mi.Text</a>
  6. } else {
  7. <a href="@mi.Href" title="@mi.Title">@mi.Text</a>
  8. }
  9. </li>
  10. }
  11. </ul>
  12.  
  13. <ul>
  14. @foreach (var mi in items) {
  15. <li @Html.Css("selected", mi.Selected)>
  16. <a href="@mi.Href" @Html.Attr("title", mi.Title)>@mi.Text</a>
  17. </li>
  18. }
  19. </ul>
  20.  
  21. public class HtmlAttribute : IHtmlString
  22. {
  23. private string _InternalValue = String.Empty;
  24. private string _Seperator;
  25.  
  26. public string Name { get; set; }
  27. public string Value { get; set; }
  28. public bool Condition { get; set; }
  29.  
  30. public HtmlAttribute(string name)
  31. : this(name, null)
  32. {
  33. }
  34.  
  35. public HtmlAttribute( string name, string seperator )
  36. {
  37. Name = name;
  38. _Seperator = seperator ?? " ";
  39. }
  40.  
  41. public HtmlAttribute Add(string value)
  42. {
  43. return Add(value, true);
  44. }
  45.  
  46. public HtmlAttribute Add(string value, bool condition)
  47. {
  48. if (!String.IsNullOrWhiteSpace(value) && condition)
  49. _InternalValue += value + _Seperator;
  50.  
  51. return this;
  52. }
  53.  
  54. public string ToHtmlString()
  55. {
  56. if (!String.IsNullOrWhiteSpace(_InternalValue))
  57. _InternalValue = String.Format("{0}="{1}"", Name, _InternalValue.Substring(0, _InternalValue.Length - _Seperator.Length));
  58. return _InternalValue;
  59. }
  60. }
  61.  
  62. public static class Extensions
  63. {
  64. public static HtmlAttribute Css(this HtmlHelper html, string value)
  65. {
  66. return Css(html, value, true);
  67. }
  68.  
  69. public static HtmlAttribute Css(this HtmlHelper html, string value, bool condition)
  70. {
  71. return Css(html, null, value, condition);
  72. }
  73.  
  74. public static HtmlAttribute Css(this HtmlHelper html, string seperator, string value, bool condition)
  75. {
  76. return new HtmlAttribute("class", seperator).Add(value, condition);
  77. }
  78.  
  79. public static HtmlAttribute Attr(this HtmlHelper html, string name, string value)
  80. {
  81. return Attr(html, name, value, true);
  82. }
  83.  
  84. public static HtmlAttribute Attr(this HtmlHelper html, string name, string value, bool condition)
  85. {
  86. return Attr(html, name, null, value, condition);
  87. }
  88.  
  89. public static HtmlAttribute Attr(this HtmlHelper html, string name, string seperator, string value, bool condition)
  90. {
  91. return new HtmlAttribute(name, seperator).Add(value, condition);
  92. }
  93. }
  94.  
  95. public static class HtmlExtensions
  96. {
  97. public static MvcHtmlString MenuItem(this HtmlHelper htmlHelper, MenuItem mi)
  98. {
  99. var li = new TagBuilder("li");
  100. if (mi.Selected)
  101. {
  102. li.AddCssClass("selected");
  103. }
  104. var a = new TagBuilder("a");
  105. a.MergeAttribute("href", mi.Href);
  106. if (!string.IsNullOrEmpty(mi.Title))
  107. {
  108. a.MergeAttribute("title", mi.Title);
  109. }
  110. a.SetInnerText(mi.Text);
  111. return MvcHtmlString.Create(li.ToString());
  112. }
  113. }
  114.  
  115. <ul>
  116. @foreach (var mi in Model.MenuItems) {
  117. @Html.MenuItem(mi)
  118. }
  119. </ul>
  120.  
  121. <ul>
  122. @Html.DisplayFor(x => x.MenuItems)
  123. </ul>
  124.  
  125. <ul>
  126. @foreach (var mi in Model.MenuItems) {
  127. <li@(mi.Selected?" class="selected"":null)>
  128. <a href="@mi.Href" @{if(!string.IsNullOrEmpty(mi.Title)) { <text>title="@mi.Title"</text>} }>@mi.Text</a>
  129. </li>
  130. }
  131. </ul>
  132.  
  133. <ul>
  134. @foreach (var mi in Model.MenuItems) {
  135. <li@(Html.Raw((mi.Selected ? " class="selected"" : null))>
  136. <a href="@mi.Href">@mi.Text</a>
  137. </li>
  138. }
  139. </ul>
  140.  
  141. <p @(cssClass != null) ? { class="@cssClass" }> Stuff and whatnot... </p>
Add Comment
Please, Sign In to add comment