Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3234268
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:22:18+00:00 2026-05-17T17:22:18+00:00

I’m quite new to both ASP.Net and MVC. I got the following code in

  • 0

I’m quite new to both ASP.Net and MVC.

I got the following code in my master page:

  <div id="main-menu" class="menu">
  <%
   var items = (IList<CompanyName.Framework.Web.MenuItem>)ViewData["MainMenu"];
   if (items.Count > 0)
   {
    %><ul><%
    foreach (var item in items)
    {
     if (!string.IsNullOrEmpty(item.RequiredRole) && !System.Threading.Thread.CurrentPrincipal.IsInRole(item.RequiredRole))
      continue;

     %><li><a href="<%= item.Uri %>"><%= item.Title %></a></li><%
    }
    %></ul><%
   }
  %>
 </div>

Can I move the code to another file or refactor the code in any way?

edit:

My ApplicationController that all controllers derive:

public class ApplicationController : Controller
{
    List<MenuItem> _mainMenu = new List<MenuItem>();
    List<MenuItem> _contextMenu = new List<MenuItem>();

    protected IList<MenuItem> MainMenu
    {
        get { return _mainMenu; }
    }

    protected IList<MenuItem> ContextMenu
    {
        get { return _contextMenu; }
    }

    protected string PageTitle { get; set; }

    protected override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        ViewData["PageTitle"] = PageTitle;
        ViewData["MainMenu"] = MainMenu;
        ViewData["ContextMenu"] = ContextMenu;
        base.OnResultExecuting(filterContext);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:22:18+00:00Added an answer on May 17, 2026 at 5:22 pm

    Here are a couple of suggestions:

    Improvement number 1: use view models and strongly typed views instead of ViewData

    public ActionResult Index()
    {
        // TODO: Fetch this data from a repository
        var menus = new[] {
            new MenuItem(), new MenuItem()
        }.ToList();
    
        return View(menus);
    }
    

    and then in your view:

    <div id="main-menu" class="menu">
        <%
            if (Model.Count > 0)
            {
                %><ul><%
                foreach (var item in Model)
                {
                    if (!string.IsNullOrEmpty(item.RequiredRole) && !System.Threading.Thread.CurrentPrincipal.IsInRole(item.RequiredRole))
                        continue;
    
                    %><li><a href="<%= item.Uri %>"><%= item.Title %></a></li><%
                }
                %></ul><%
            }
        %>
    </div>
    

    Still horrible and completely unreadable tag soup.


    Improvement number 2: use editor/display templates:

    In ~/Views/Home/DisplayTemplates/MenuItem.ascx:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CompanyName.Framework.Web.MenuItem>" %>
    
    <% if (!string.IsNullOrEmpty(Model.RequiredRole) &&
           System.Threading.Thread.CurrentPrincipal.IsInRole(Model.RequiredRole)) { %>
        <li>
            <a href="<%= Model.Uri %>"><%= Model.Title %></a>
        </li>
    <% } %>
    

    And then in your main view:

    <div id="main-menu" class="menu">
        <ul>
            <%= Html.DisplayForModel() %>
        </ul>
    </div>
    

    Improvement number 3: Avoid coding business rules in a view. So in your view model add a property:

    public bool IsLinkVisible
    {
        get
        {
            return !string.IsNullOrEmpty(RequiredRole) &&
                   Thread.CurrentPrincipal.IsInRole(RequiredRole);
        }
    }
    

    so that your display template now looks like this:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CompanyName.Framework.Web.MenuItem>" %>
    <% if (Model.IsLinkVisible) { %>
        <li>
            <a href="<%= Model.Uri %>"><%= Model.Title %></a>
        </li>
    <% } %>
    

    Improvement number 4: Write a custom HTML helper to render this anchor because writing C# in a view is still ugly and untestable:

    public static class HtmlExtensions
    {
        public static MvcHtmlString MenuItem(this HtmlHelper<MenuItem> htmlHelper)
        {
            var menuItem = htmlHelper.ViewData.Model;
            if (!menuItem.IsLinkVisible)
            {
                return MvcHtmlString.Empty;
            }
            var li = new TagBuilder("li");
            var a = new TagBuilder("a");
            a.MergeAttribute("href", menuItem.Uri);
            a.SetInnerText(menuItem.Title);
            li.InnerHtml = a.ToString();
            return MvcHtmlString.Create(li.ToString());
        }
    }
    

    and finally your display template:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CompanyName.Framework.Web.MenuItem>" %>
    <%= Html.MenuItem() %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.