Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Layout

Layout:

1.  Layout  are like master pages in ASP.NET Web form. Master pages give a standard look and feel for Web form views while layout gives standard look and feel or acts like a template for razor views.
2.  Layouts are used to maintain a consistent look and feel across multiple views within ASP.NET MVC application.

 In MVC application layout is created below shared folder, following image shows details


Example:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
 
    <!--site design or layout css-->
   <link href="@Url.Content("~/Content/style.css")" rel="stylesheet" type="text/css" />
     
    <!--Jquery theme css-->
   <link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" />
 
    <!--menu css-->
    <link href="@Url.Content("~/Content/superfish.css")" rel="stylesheet" type="text/css" />
  
        <!--Default js-->
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.js")" type="text/javascript"></script>
 
 
        <!--menu js-->
    <script src="@Url.Content("~/MyScripts/hoverIntent.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/MyScripts/superfish.js")" type="text/javascript"></script>
  
  
 <script type="text/javascript">
 
     //button
     $(function () {
         $("input[type=submit],a,button")
                                    .button()
                                    .click(function (event) {
                                        //                                 event.preventDefault();
                                    });
     });
 
     //corner all code
     $(function () {
         $('input').addClass("ui-corner-all");
     });
            </script>
 
</head>
<body>
<div class="main">
  <div class="header">
    <div class="header_resize">
      <div class="logo"><h1><span>Easy</span> Leave<small>HR For All</small></h1>
         
      </div>
     <!--menu code -->
      <div class="menu">
       <ul>
         <li><h3>About us</h3></li>
          <li><h3>Products</h3></li>
          <li><h3>Contact us</h3></li>    
          <li>
          </li>
          <li> <div id="logindisplay">
         @Html.Partial("_LogOnPartial") </div></li>
        </ul>
 
 
      </div>
      <div class="text_header">
     @if (Request.IsAuthenticated)
     {
        <nav>
<ul class="sf-menu" id="example">
                                      @if (User.IsInRole("Admin") || User.IsInRole("Superadmin"))
                    {
              
    <li class="current">
 
              
                                                <a href="#">Admin</a>
                <ul>
                <li>
                <a href="#">Employee</a>
                <ul>
                  
                     <li>@Html.ActionLink("Employee", "Index", "Employee")</li>
                                                    <li>@Html.ActionLink("Designation", "Index", "Designation")</li>
                    <li>@Html.ActionLink("Role", "Index", "Role")</li>
                   <li>@Html.ActionLink("Change Password", "ChangePassword", "Account")</li>
                
                    </ul>
                </li>
                <li>
                                                                        <a href="#">Company Masters</a>
                                                                        <ul>
         
                                                                                      <li>@Html.ActionLink("Company", "Index", "Company")</li>
                                                                                     <li>@Html.ActionLink("Department", "Index", "Department")</li>
         
                                                                        </ul>
                                       </li>
                                        </ul>
                </li>
       }
           <li class="current">
                                                <a href="#">Reports</a>
                                                <ul>
                                                <li>
                                                    @Html.ActionLink("Dash Board", "DashBoard", "Reports")
                                                </li>
                 </ul>
                                    </li>
                                    <li>
 
                <a href="#">Configuration</a>
                <ul>
                                    <li>
                                                                        @Html.ActionLink("Holiday List", "Index", "LeaveMaster")
                                                            </li>
                                                    <li>@Html.ActionLink("Comp Off", "Index", "CompOff")</li>
                                        </ul>
                                    </li>    
 
                        </ul>
        </nav>
     }
   <!--menu code ends here -->
      </div>
      <div class="clr">
              
           </div>
    </div>

    <div class="headert_text_resize_bg">
      <div class="headert_text_resize">  @RenderBody()
        <div class="textarea">
    
        </div>
      </div>
    </div>
    <div class="clr"></div>
  </div>
 
 
 
  <div class="body">
    <div class="body_resize">
    
   
      <div class="clr"></div>
    </div>
  </div>
    
  <div class="clr"></div>
  <div class="footer">
    <div class="footer_resize">
       <footer>
       © Copyright ,Pune 2015
        </footer>
 
    </div>
    <div class="clr"></div>
  </div>
</div>
</body>
</html>
 

 

Using Layout in view:

You can use a layout to define a common template for your site. A layout can be declared at the top of view as

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

OR

While creating view select check box option “Use Layout or master page”

Select proper layout in URL or path section, following image explains in detail. 


MVC application life cycle

MVC application life cycle

Point to Remember:
There is nothing like MVC application life cycle.To be specific the MVC request goes through various steps of execution & that’s what is termed as MVC application life cycle.

 Any web application has two main execution steps

1.  Request (HTTP Request)
2.  Response (HTTP Response)

MVC application life cycle is not different it has two main phases first creating the request object and second sending our response to the browser.

Creating the request object:  The request object creation has four major steps.

Following Steps explains in Detail:

Step 1 Fill route:  MVC requests are mapped to route tables which in turn specify which controller and action to be invoked. So if the request is the first request the first thing is to fill the route table with routes collection. This filling of route table happens in the global.asax file.

Step 2 Fetch route: Depending on the URL sent “UrlRoutingModule” searches the route table

to create “RouteData” object which has the details of which controller and action to invoke.

Step 3 Request context created:  The “RouteData” object is used to create the “RequestContext” object.

Step 4 Controller instance created:  This request object is sent to “MvcHandler” instance to create the controller class instance. Once the controller class object is created it calls the “Execute” method of the controller class.

Creating Response object:   This phase has two steps executing the action and finally sending the response as a result to the view.

Step 1 Execute Action:  The “ControllerActionInvoker” determines which action to executed and executes the action.

Step 2 Result sent:  The action method executes and creates the type of result which can be a view result, file result, JSON result etc.

So in all there are six broad steps which get executed in MVC application life cycle.

Note:  In case you are not able to remember the above steps remember the acronym FFRCER (Fight For Respect Can Evoke Revolution).

 Example:

Route table for execution path in global.ascx

  public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
 
            routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } //Parameter defaults
            );
 
        }

Register route :

protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }

Suppose your web application is running on www.codechef4u.com then the url pattren for you application will be www.codechef4u.com /{controller}/{action}/{id}.

i.e  http:// codechef4u.com/Account /UserDateils /1

Route explains:

Controller:  Account
Action : UserDateils
Value :1

MVC routing

In this article we explained in detail important asp.net MVC feature called routing.

The ASP.NET Routing module is responsible for mapping incoming browser requests to particular MVC controller actions.At runtime, Routing engine use the Route table for matching the incoming request's URL pattern against the URL patterns defined in the Route table.

Using the default route table:

A route table is created in the application's Global.asax file. The Global.asax file is a special file that contains event handlers for ASP.NET application lifecycle events. The route table is created during the Application Start event.

public static void RegisterRoutes(RouteCollection routes)
{
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");      
 
     routes.MapRoute("Default", // Route name
     "{controller}/{action}/{id}", // URL with parameters
      new { controller = "Account", action = "LogOn", id = UrlParameter.Optional              } // Parameter defaults(Default values for parameters)
);
 
        }


How it works:

When an MVC application first starts, the Application_Start() method is called. This method, in turn, calls the RegisterRoutes() method. The RegisterRoutes() method creates the route table.

1.  Default route table maps first part of URL to controller name, in above example that’s Default.
2.  Default route table maps second part of URL to controller action in above example default action is logon.
3.  Default route table maps third part of URL to id that’s optional.

Example:

If you enter the below URL into your web browser's address bar:

  www.elave.com/LeaveManager/Detail/2

 The Default route maps this URL to the following parameters:

     a.  controller = LeaveManagerController
b.  action = Detail
c.  id = 2

When you request the www.elave.com/LeaveManager/Detail/2, the following code is executed:

LeaveManagerController.Detail(2)


Default values in above example:

If you don't supply a controller, then the controller parameter defaults to the value Account. If you don't supply an action, the action parameter defaults to the value LogOn. Finally, if you don't supply an id, the id parameter defaults to an empty string.

Example with DepartmentController:

Requests by user:
1.  www.leave-example.com/Department/index (index action with no parameter)
2.  www.leave-example.com/Department/Details/1 (Details action with parameter value 1)

In following code you can check two actions Index with no parameter value and Detail with parameter value.

   

   public class DepartmentController : Controller
    {
        private UnituOfWork unitOfWork = new UnituOfWork();
        //
        // GET: /Leave/
 
        public ActionResult Index()
        {
            var Departments = unitOfWork.GetAllDepartments();
            return View(Departments);
        }
 
 
       
        public ActionResult Detail(int id)
        {
            Department Departmentr = unitOfWork.departmentRepository.GetByID(id);
 
            return View(Departmentr);
 
        }
 
        public Guid GetUser()
        {
            MembershipUser user = Membership.GetUser(HttpContext.User.Identity.Name);
            Guid userid = (Guid)user.ProviderUserKey;
            return userid;
        }
 
    }