Create controller base class (partial)

3.5k Views Asked by At

Since my @html.render action crashes my dev and prod servers i have to use partials(crap).

I tried creating public partial controller{} class so i can set needed data for all my views but i am having no luck (everything breaks).

I am coming from LAMP cakePHP background and really need simplicity.

I need to know how to create a partial base controller(that doesnt override the regular base controller) and how to access multiple models from the class.

Thank you!

2

There are 2 best solutions below

5
On
public class BaseController: Controller
{
   public override OnActionExecuting(...) { ... }
   public override OnActionExecuted(... context) 
   {
       if (context.Result is ViewResult)
           ((ViewResult)context.Result).ViewData["mycommondata"] = data;
   }
   ...
}

public class MyController1: BaseController 
{
}

I.e. just derive from your new base controller class.

However I'd suggest you to ask here why your RenderPartial "crashes" - since it can be a better way for you, and it obviously shouldn't crash.

0
On

better way to create base controller

    public class Controller : System.Web.Mvc.Controller
{
    public shipsEntities db = new shipsEntities();

    public Controller()
    {
        ViewData["ships"] = db.ships.ToList();
    }
}

that way the rest of controllers follow regular convention

public class MyController : Controller