gs11111
gs11111

Reputation: 659

Insert _ViewStart.cshtml

Is that possible to use viewstart only for a particular controller and view? I was using only _Layou.cshtml file inside views folder. Now i added _ViewStart.cshtml as common view inside views folder and moved _Layout to Shared folder.

This is program structure:

Homecontroller
  public ActionResult Index()
        {
            return View();
        }

Index.cshtml
@{
    Layout = "~/Views/_Layout.cshtml";
}

_Layout.cshtml
{
//design code for Index.chtml
}

as per the above code, _Layout rendered for homecontroller . When done the changes mentioned at the very first line, I'm getting the controls inside _Layout.cshtml in every controller I use. I use nearly 6 controllers. How to make this change without disturbing the entire code. Please help.

PS: I need to introduce _ViewStart into the program since I'm integrating openid with my already developed project.

Upvotes: 2

Views: 2857

Answers (2)

haim770
haim770

Reputation: 49095

  1. You can create another _ViewStart.cshtml (in Views/[controller] a sub-folder for example) that will override the root one, something like:

    @{ Layout = null; }

  2. You can simply use the ViewBag to determine whether to use Layout or not:

    public ActionResult AnotherAction()
    {
         ....
         ViewBag.NoLayout = true;
    
         return View();
    }
    

    and in your _ViewStart:

    @{
    if (ViewBag.NoLayout == null || !ViewBag.NoLayout)
         Layout = "~/Views/_Layout.cshtml";
    }
    

Upvotes: 2

Nick
Nick

Reputation: 4212

You can read more about MVC3 Razor layouts on Scott Guthrie's Blog

Upvotes: 1

Related Questions