Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <ul>
- @foreach (var mi in Model.MenuItems) {
- <li@(mi.Selected?" class="selected"":null)>
- @if (string.IsNullOrEmpty(mi.Title)) {
- <a href="@mi.Href">@mi.Text</a>
- } else {
- <a href="@mi.Href" title="@mi.Title">@mi.Text</a>
- }
- </li>
- }
- </ul>
- <ul>
- @foreach (var mi in items) {
- <li @Html.Css("selected", mi.Selected)>
- <a href="@mi.Href" @Html.Attr("title", mi.Title)>@mi.Text</a>
- </li>
- }
- </ul>
- public class HtmlAttribute : IHtmlString
- {
- private string _InternalValue = String.Empty;
- private string _Seperator;
- public string Name { get; set; }
- public string Value { get; set; }
- public bool Condition { get; set; }
- public HtmlAttribute(string name)
- : this(name, null)
- {
- }
- public HtmlAttribute( string name, string seperator )
- {
- Name = name;
- _Seperator = seperator ?? " ";
- }
- public HtmlAttribute Add(string value)
- {
- return Add(value, true);
- }
- public HtmlAttribute Add(string value, bool condition)
- {
- if (!String.IsNullOrWhiteSpace(value) && condition)
- _InternalValue += value + _Seperator;
- return this;
- }
- public string ToHtmlString()
- {
- if (!String.IsNullOrWhiteSpace(_InternalValue))
- _InternalValue = String.Format("{0}="{1}"", Name, _InternalValue.Substring(0, _InternalValue.Length - _Seperator.Length));
- return _InternalValue;
- }
- }
- public static class Extensions
- {
- public static HtmlAttribute Css(this HtmlHelper html, string value)
- {
- return Css(html, value, true);
- }
- public static HtmlAttribute Css(this HtmlHelper html, string value, bool condition)
- {
- return Css(html, null, value, condition);
- }
- public static HtmlAttribute Css(this HtmlHelper html, string seperator, string value, bool condition)
- {
- return new HtmlAttribute("class", seperator).Add(value, condition);
- }
- public static HtmlAttribute Attr(this HtmlHelper html, string name, string value)
- {
- return Attr(html, name, value, true);
- }
- public static HtmlAttribute Attr(this HtmlHelper html, string name, string value, bool condition)
- {
- return Attr(html, name, null, value, condition);
- }
- public static HtmlAttribute Attr(this HtmlHelper html, string name, string seperator, string value, bool condition)
- {
- return new HtmlAttribute(name, seperator).Add(value, condition);
- }
- }
- public static class HtmlExtensions
- {
- public static MvcHtmlString MenuItem(this HtmlHelper htmlHelper, MenuItem mi)
- {
- var li = new TagBuilder("li");
- if (mi.Selected)
- {
- li.AddCssClass("selected");
- }
- var a = new TagBuilder("a");
- a.MergeAttribute("href", mi.Href);
- if (!string.IsNullOrEmpty(mi.Title))
- {
- a.MergeAttribute("title", mi.Title);
- }
- a.SetInnerText(mi.Text);
- return MvcHtmlString.Create(li.ToString());
- }
- }
- <ul>
- @foreach (var mi in Model.MenuItems) {
- @Html.MenuItem(mi)
- }
- </ul>
- <ul>
- @Html.DisplayFor(x => x.MenuItems)
- </ul>
- <ul>
- @foreach (var mi in Model.MenuItems) {
- <li@(mi.Selected?" class="selected"":null)>
- <a href="@mi.Href" @{if(!string.IsNullOrEmpty(mi.Title)) { <text>title="@mi.Title"</text>} }>@mi.Text</a>
- </li>
- }
- </ul>
- <ul>
- @foreach (var mi in Model.MenuItems) {
- <li@(Html.Raw((mi.Selected ? " class="selected"" : null))>
- <a href="@mi.Href">@mi.Text</a>
- </li>
- }
- </ul>
- <p @(cssClass != null) ? { class="@cssClass" }> Stuff and whatnot... </p>
Add Comment
Please, Sign In to add comment