IMG-LOGO

How to set layout template in Controller class in MVC?

andy - 29 Dec, 2015 3594 Views 0 Comment

Normally when you want to set a separate layout in MVC, you need to set this manually in the cshtml file. Alternatively, you can now set it automatically in behind code according to specific condition you want.

See the following code example.

var layout = "";
if(condition1){
     layout = "_firstLayout";
}else if(condition2){
     layout = "_secondLayout";
}

//on your return view at the end of the code, you can use the following code.
 return View("index", layout ); 

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.

Related Articles

ASP.Net MVC Identity without Entity Framework

Learn how to create your own custom identity authentication and authorization with ASP Net MVC without using Entity Framework By default the example given in the MVC official tutorial site is using Entity Framework So if you do not want ...

How to enable attribute routing in C# MVC?

p If you want to enable routing in C MVC you have to do the following steps Note this only applies to strong MVC version 5 or above strong Open your RouteConfig cs file under App_Start folder in your MVC ...

PayPal Express Checkout using C# MVC Web API

p In this tutorial you will learn how easily you can implement a simple checkout express using C MVC Web API We will create a really simple shopping cart where customers can add and delete their cart items before proceed ...