Kumar
Kumar

Reputation: 11329

asp.net mvc change master page & .css dynamically

Is there a good way to change the MasterPage and/or .css dynamically in asp.net mvc based on the user preferences?

I understand I can change the master name as follows:

return View("viewName", "master-name", oModel)

and the view using a different contentPlaceHolder perhaps but that requires changing each controller+action.

I'd have to assume there is to be a better way than this.

Upvotes: 4

Views: 4486

Answers (2)

James S
James S

Reputation: 3374

I have a somewhat simpler method:

return View("View", getMasterName());

and in my master controller, I have:

protected string getMasterName() {
    return (Request.QueryString["tb"] == null) ? null : "Other_Master";
}

I use it to display a different template in the case of a thickbox popup vs if, eg, javascript isn't working and the controller is loaded without thickbox.

Upvotes: 3

Related Questions