Dynamic site.master menu

I need help with the following:

I just read this tutorial: http://www.asp.net/LEARN/mvc/tutorial-13-cs.aspx

and followed the example given for "good solution". However, I ran into the problem after making some changes to the abstract ApplicationController class.

The original constructor was:

public ApplicationController()
{                
    ViewData["categories"] = from c in DataContext.MovieCategories
                             select c;           
} 

      

I changed it to use a stored procedure instead:

public ApplicationController()
{                
    ViewData["categories"] =  DataContext.spMovieCategories("some movie category");
} 

      

Anyway, can I pass "some movie category" as a parameter? I tried using TempData and Session variables, but it keeps throwing errors at me.

Thanks guys!

0


source to share


3 answers


If you need dynamic menu in asp.net follow these steps.

  • Gets a visualization from a database.
  • The administrator can change the name of the menu by simply updating the database table. For example, Office org for My Org. and etc.
  • You don't want to hard-code the redirect url for any menu click (i.e. entering the url in the database as well).
  • I just don't want to have an xml file for the above things. (on fly xml variable construction and distruction, which avoids creating an XML file on the server).

  • I just don't want to do jquery



Then click here Dynamic Menu

+1


source


You can use a parameter in the constructor to pass it:

public ApplicationController(string movieCategory)
{
    ViewData["categories"] = DataContext.spMovieCategories(movieCategory);
}

      

Obviously, you will need to provide this from derived controllers, which can be a problem if the category is not specific to each derived controller, which seems unlikely:

public DerivedController() : base("Derived Movie Category")
{
    // ...
}

      

It is most likely best to move the category search out of the constructor and move it to a separate method in the ApplicationController. The easiest way to get this call is to then insert a call to it in every action after you have a category parameter (am I taking it as one of the Action call parameters?). It's a bit of a pain though.

The supposed way to solve this problem is ActionFilters if you create a new ActionFilter like this:

public class CategoryAttribute : ActionFilterAttribute, IActionFilter
{
    #region IActionFilter Members

    void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
    {
        string category = (string)filterContext.RouteData.Values["category"];
        ((ApplicationController)filterContext.Controller).PopulateCategory(category);
    }

    void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
    {
    }

    #endregion
}

      



Any action with the attached attribute Category

will execute this code. (I assumed you moved the code in ApplicationController

to a method called PopulateCategory

. You can also apply it at the controller level, which will then call this ActionFilter for every action in the controller.

Since you want it to be called for every action on every controller, you can apply it to your ApplicationController so that every derived controller inherits it:

[Category]
public class ApplicationController : Controller
{
    // ...
}

      

[Edit - Slightly better solution]

However, another step towards simplification is to not use the attribute, but instead override the controller's OnActionExecuted method (which I only noticed after I wrote this answer).

This means that you can get rid of the class CategoryAttribute

and Category

the class ApplicationController

and just add it to the class ApplicationController

:

protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
    string category = (string)filterContext.RouteData.Values["category"];
    this.PopulateCategory(category);
    base.OnActionExecuted(filterContext);
}

      

0


source


I did this by creating my own ViewDataPage class that inherits from System.Web.Mvc.ViewDataPage.

namespace myWebsite.Helpers
{
public class ViewMasterPage : System.Web.Mvc.ViewMasterPage
{
    public bcData Data = new bcData();

}
}

      

Modify web.config to include your namespace:

  <namespaces>
    <add namespace="betClubUk.Helpers" />
  </namespaces>

      

Then, implement the data in the view.

<%@ Master Language="C#" Inherits="betClubUk.Helpers.ViewMasterPage" %>

<%=Data.getVariableContent().statOfDay.text %>

      

0


source







All Articles