views:

529

answers:

1

Hello There,

By any chance, is there any easy way to set a default MasterView for all the actions inside a specific controller?

For example If I have the HomeController I want all the actions inside it to inherit the Site.Master as default, but If I am inside AccountsController I want all the action to inherit the Admin.Master and so on..

I managed to do it with:

return View("viewName", "masterName", objectModel);

But by doing this I have to apply it every time I call the View method.

I was looking for something simpler like on rails where we can declare:

class HomeController < ApplicationController

  layout 'site'

   def index
   end

   def create
   ...

end

class AccountsController < ApplicationController

  layout 'admin'

  def index
  end

  def create
  ...  

end

Is that possible?

Thanks in advance

+3  A: 

You could override OnActionExecuting in that Controller class.

protected override void OnActionExecuting(ActionExecutingContext filterContext) 
{ 
    ViewData["MasterfileToUser"] = "site";
}

Or if you like you can turn this into an ActionFilterAttribute you can apply on the controller or action level

using System;
using System.Web.Mvc;
public class MasterFileFilterAttribute : ActionFilterAttribute
{
    public string Master { get; set; }

    public override void OnActionExecuted( ActionExecutedContext filterContext)   
    {        
            if (filterContext.Result is ViewResult)
                    ((ViewResult)filterContext.Result).MasterName = Master;
    }
}

which you then in turn use like so:

[MasterFileFilterAttribute(Master = "site")] 
public class HomeController : Controller 
{ 
    // Action methods 
}
olle
Hi Olle, thanks for your answer. Just One thing that I didn't catch, is how a ViewData["MasterfileToUser"] reflects the Master file? Is that anything else that I should do to make it load the Master File Properly?
ludicco
My bad assigning it from viewdata won't work. I took a second stab but I am currently not somewhere where I can test this. In OnActionExecuted the result (a ViewResult instance) is available it has a Masterpage property so I am now assigning it there. Please test yourself :)
olle
Olle Just one think here...Now Everytime I call the method RedirectToAction it throws an error:Unable to cast object of type 'System.Web.Mvc.RedirectToRouteResult' to type 'System.Web.Mvc.ViewResult'.And acuses the MasterFileFileAttribute class. Any ideas on how to solve it?Thanks
ludicco
Hy Olle, I found the answer! :)I'm very new with this C# thing so there's a lot of things that I don't know how to do (like 'unless' from ruby (didn't worked with (!...)) but it worked using the following code over your class: http://pastebin.com/mf6d587eI found some direction to this at: http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspxFeel free to update your answer with a revised code if you want.Thanks again
ludicco
olle
That's perfect Olle. So I think the question is answered!Thanks a lot for you help! All the best
ludicco