Mayur Deore's Blog

Post info:

ASP .NET MVC 5 and basics of MVC

 

mvc5logo

 

This article is about What is MVC? and some important components about ASP .NET MVC.

I’ve started Open-Source E-Commerce project in ASP .NET MVC 5 you can read more about it here. Before moving forward,let’s understand about MVC.

 

The MVC stands for Model View Controller. Let’s discuss about MVC pattern in Web Application.

 

Why MVC is gaining popularity for Web Development?

The working of MVC Application is as follows:

1: User takes action and send request to application.  (Controller)

2: The application then process data according to the request of user (Model)

3: The application then update view and send as response to the user (View)

 

Now let’s consider working of Web Application. User perform event on web-page,request is then sent to server (like HTTP request),server performs some operation and incorporate those changes in web-page and send that updated page to the user as response.

 

Now compare both working concept, we can say that working of MVC application and Web Application are somewhat same. That’s the main reason, Microsoft came up with ASP .NET MVC with improved separation of concern.

 

The MVC has three pieces:

  1. Models : Represent data that user works with. Mainly contains Entity class. Models doesn’t know anything about controller or view. So, we can use Models in any other application as well.
  2. Views : Represent the user interface, which will be rendered to represent models in UI.
  3. Controllers: Process request coming from user, performs operations on Models and select Views to render to user.

 

In ASP .NET MVC, Controllers and Models are C# Classes. Controllers class have method returning action method to return ActionResult objects which are view,content,partial view,JSON or empty model. Views are generated by View Engine of ASP .NET MVC Framework. MVC 3 introduced Razor view Engine, which uses different syntax for views.

 

The simple example is as follows (from E-CommerceMVC):

Model:- Product.cs

 

namespace LetsShop.Models
{
    public class Product
    {
        public int ID { get; set; }
        public string Name { get; set; }

    }
}

 

Controller:-ProductController.cs

using System.Web.Mvc;
using LetsShop.Models;

namespace LetsShop.Controllers
{
    public class ProductController : Controller
    {
        // GET: Product
        public ActionResult Index()
        {
            var product = new Product { Name = "Mobiles" };

            return View(product);
        }
        
    }
}

 

View: Index.cshtml

@model LetsShop.Models.Product
@{
    ViewBag.Title = "Index";
}

<h2>@Model.Name</h2>

 

Apart from Model,View and Controller, In ASP .NET MVC there is one more component which is as important as these 3, viz. Router. The Router is responsible for where to send user request i.e Controller and Method of that controller.

 

The sample RouteConfig.cs is:

using System.Web.Mvc;
using System.Web.Routing;

namespace LetsShop
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

 

In ASP .NET default url is of form: Controller/Action/Id.

For above example the url will be: Product/Index 

(Index is optional as default value for action is Index, And Index method of ProductController doesn’t need any parameter so ID is excluded from url).

We can specify the custom route in App_Start/RouteConfig.cs in ASP .NET MVC.

 

The output of above simple example is:

 

MVCSimpleOutPut

 

 

There are much more topics to write about in ASP .NET MVC, which I’ll write as our E-CommerceMVC project develops. Please Like Facebook page for upcoming updates about Just Code Web.

If you have any suggestions/queries please comment below.

Mayur Deore

Facebook Twitter LinkedIn 

Leave a Reply

Your email address will not be published. Required fields are marked *