Friday, 4 November 2016

MVC Overview and Controller and View Example

Model View Controller (MVC)
It is an architectural pattern that divides an application into three main components: the model, the view, and the controller. It is used for creating Web applications.

The MVC framework includes the following components:

  1. Model: It implements the logic for the application's data domain. Model objects retrieve and store model state in a database.
  2. View: It is responsible for User Interface. The interface that will be shown to user.
  3. Controller: It handles the user interaction, work with the model and select the underlying view which needs to be shown to user.

Advantages of MVC

  1. No View State and Server side controls in MVC. This will reduce the response time from server and also the bandwidth because page size is reduced and no conversion time required from asp controls to HTML controls because we are using HTML Controls.
  2. Easy manageable by dividing an application into the model, the view, and the controller
  3. Parallel development can be done in a better way. Business team can build the data logic, while t UI developers can design User Interface simultaneously,
Controller Example

Step 1 : Create new MVC Project

Step 2 : Change the Name, Location of Project


Step 3 : Choose Empty MVC Application


Step 4 : Create Controller







Step 5 : Put the given Code in Controller and Press F5


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCExample.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public string Index()
        {
            return "This is Controller Example";
        }
       }
}

 View Example

Step 1 : Right click index and select Add View



Step 2 : Index view will be created in Home Folder inside Views Folder.
Put the following code in View and press F5





@{
    ViewBag.Title = "Index";
}

<h2>This is a View</h2>

 Step 3 : If below error occurs than follow the given steps


Step 4 :  Go to Manage Nuget Package and Install/Update Microsoft ASP.NET MVC





Step 5 : Run the Application


0 comments:

Post a Comment