views:

35

answers:

1

I have a class MyController that inherits from Controller, so all my controllers inherit from MyController.

I have a property in MyController:

public class MyController : Controller
{
    public string SomeProperty {get;set;}
}

if I set this property in MyController's OnExecuting method, my HtmlHelper extension method works fine:

public static string SomeExtension(this HtmlHelper htmlHelper)
{
    StringBuilder sb = new StringBuilder();            
        string result = "";
    var controller = htmlHelper.ViewContext.Controller as MyController;
    if (controller != null)
    {
        result = controller.SomeProperty;
    }

       return result;
}

it doesn't work if I set 'SomeProperty' in my controllers action method.

I guess because I am doing 'as MyController' in the extension method?

is there a way for it to work in both situations?

I am using the value of SomeProperty to be outputted on my view pages.

A: 

Don't use properties on a base controller to pass data to the view. I would recommend you writing a custom action filter and store data inside ViewData which will be accessible everywhere.

Darin Dimitrov