Thursday, June 13, 2013

How can we navigate from one view to other view using hyperlink?

By using “ActionLink” method (in Razor) as shown  in the below code. The below code will create a simple URL which help to navigate to the “Home” controller and invoke the “GoHome” action.

@Html.ActionLink("Home","Gohome")

routing in MVC

Routing helps you to define a URL structure and map the URL with the controller.
For instance let’s say we want that when any user types “http://localhost/View/ViewCustomer/”,  it goes to the  “Customer” Controller  and invokes “DisplayCustomer” action.  This is defined by adding an entry in to the “routes” collection using the “maproute” function. Below is the under lined code which shows how the URL structure and mapping with controller and action is defined.

sample routing in Global.asax:

routes.MapRoute(
               "View", // Route name
               "View/ViewCustomer/{id}", // URL with parameters
               new { controller = "Customer", action = "DisplayCustomer", 
               id = UrlParameter.Optional }); // Parameter defaults 

complete flow of MVC

  • All end user requests are first sent to the routing in global.asax
  • Then All requests are sent to the controller.
  • The controller depending on the request decides which model to load. The controller loads the model and attaches the model with the appropriate view.
  • The final view is then attached with the model data and sent as a response to the end user on the browser.

what is the advantage of MVC instead of WebForms


MVC allows the user to write less amount of code to build the web applications as lots of components are integrated to provide the flexibility in the program.

The application tasks are separated into components to make it easier for the programmers to write the applications but at the same time the amount of the code also increases. 

Main focus remains on the testability and maintainability of the projects that is being carried out in large projects. 

Tuesday, June 4, 2013

Controller in MVC4 sample and explanation

To create an empty controller, right-click on the Controllers folder and select Add → Controller…,
entering the name HomeController.
The wizard will ask you if you’d like it to generate Create, Update, Delete, and Details actions for you. We won’t use those actions in this book, but feel free to let the wizard generate them for you if you’re interested to see what they look like. The wizard will create a new class file with one action named Index:

using System.Web.Mvc;
namespace MvcRazorBlog.Controllers
{
     public class HomeController : Controller
    {
         public ActionResult Index()
        {
            return View();
        }
    }
}

According to ASP.NET MVC’s default routes, the HomeController’s Index action handles requests for the site’s home page (the root of the site without specifying any file or folder names).
So, the next step is to reproduce the same data access logic that the Default.cshtml page uses to retrieve Post data.

Model in MVC sample and explanation


       
        ASP.NET MVC’s architecture dictates that the Model and the View are two separate entities, so in order to demonstrate the Razor syntax within ASP.NET MVC’s Razor View Engine, you should create a model that can hold and manage the site’s data. Since you’ve already implemented the blog site once, you already know what data the site uses. To create the Model for the ASP.NET MVC blog site, add a new class named Post to the website’s Models folder, with the following code:

namespace MvcRazorBlog.Models
{
 public class Post
 {
      public long ID { get; set; }
      public string Title { get; set; }
      public string Body { get; set; }
 }
}

Since the blog site doesn’t require very complex data, the Post class is all that’s needed at this point. Once it’s in place, you can create the Controller that will populate the Post class with data from the database and pass it to the View.

The Model-View-Controller Architecture


The MVC architecture comprises three layers, each with unique and independent responsibilities:

Model
       Represents the core business/domain data and logic, typically with POCOs (Plain Old CLR Objects), devoid of technology-specific implementations

View
       Responsible for transforming a Model or Models into a response sent to the user (typically HTML)

Controller 
       Interprets incoming web requests, managing the interaction between the user and the model (typically through database queries, web services calls, etc.) and building a Model for the View to consume
   
       In the course of an ASP.NET MVC website request, the platform locates and executes the  corresponding controller method, also called the action. The result of this action is almost always an ActionResult. The most widely used type is the ViewResult—an ActionResult indicating which view the framework should respond to the request with. Following ASP.NET MVC’s strict separation of concerns, it is not the controller that is responsible for rendering HTML. Instead, the ASP.NET MVC framework passes the ActionResult from the controller to the View Engine, which handles the conversion of a ViewResult into rendered HTML to send back to the client.