Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use a parameter in the constructor to pass it in:</p> <pre><code>public ApplicationController(string movieCategory) { ViewData["categories"] = DataContext.spMovieCategories(movieCategory); } </code></pre> <p>Obviously, you'd need to be able to supply this from the derived controllers, which may be an issue unless the category is specific to each derived controller which seems unlikely:</p> <pre><code>public DerivedController() : base("Derived Movie Category") { // ... } </code></pre> <p>What is probably best is to move the categories retrieval out of the constructor, and move it into a separate method in ApplicationController. The <em>simplest</em> way of getting this called is then to insert a call to it in each Action once you have the category parameter (I'm assuming it's one of the parameters to the Action call?). This is a bit of a pain though.</p> <p>The intended way to solve this problem is ActionFilters, if you create a new ActionFilter like this:</p> <pre><code>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 } </code></pre> <p>Any action with the <code>Category</code> attribute applied will execute this code. (I've assumed that you've moved the code in <code>ApplicationController</code> into a method called <code>PopulateCategory</code>. You can also apply it at the Controller level, which then will cause this ActionFilter to be called for every action in the controller.</p> <p>Since you want it to be called for <em>every</em> action on <em>every</em> controller, you can apply it to your ApplicationController so that every derived controller will inherit it:</p> <pre><code>[Category] public class ApplicationController : Controller { // ... } </code></pre> <p>[Edit - Slightly better solution]</p> <p>However, an even further step to simplify is to not use an attribute, but instead to override the OnActionExecuted method of Controller (that I just noticed after I wrote this answer).</p> <p>That means you can get rid of the <code>CategoryAttribute</code> class and the <code>Category</code> attribute on the <code>ApplicationController</code> class and just add this to the <code>ApplicationController</code> class:</p> <pre><code>protected override void OnActionExecuted(ActionExecutedContext filterContext) { string category = (string)filterContext.RouteData.Values["category"]; this.PopulateCategory(category); base.OnActionExecuted(filterContext); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload