Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

This tutorial will help you to get started with ASP.NET MVC

Getting started with ASP.NET MVC

Getting Started with ASP.NET MVC - Part 3: Controller basics, Action Results, Models and View

May 1 2012 12:00AM by Jalpesh   

In the previous chapter, we have learned about how we ASP.NET MCV application works. In this chapter we are going to learn about controller, Action Result, Models and view.

Controller:

As we have learned in earlier chapter that Controller processes incoming requests and once request completes, it will execute the logic that we have written in controller and then it will call View to render html as a response.

In MVC, any controller class will be inherited from “Controller” base class, which will be responsible for handling all the requests from the user inputs and user instructions. It performs following operations:

  1. Based on the request, it will locate action result locate methods and it will validate request before passing it to Action Result methods
  2. Once execution completes, it will call a view to render output as HTML.
  3. It is also responsible for executing application logic that is written in Action Result Method.

Following is a typical example of code for controller class.

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

namespace MyFirstMVC.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }
    }
}

As you can see in above example, there are two action result methods written. Based on the URL, Controller will determine which method will be executed and once it will complete the execution, it will render the view for that particular action result as a response. In the above example, you can see that there are two Action Result methods Index and About and both are returning their respective views.

Action Result:

Action result methods have one-to-one mapping with user interactions and user requests. It contains all the logics that are required to process for the any request related to it and in response, it will return respective view. Base URL entered by user controller will determine which Action Result Methods will call.

Following is example of Action Result Method:

public ActionResult Index()
{
      ViewBag.Message = "Welcome to ASP.NET MVC!";

      return View();
}

Here you can see, the ActionResult Method name is Index. So once user will enter URL as Index in the browser MVC routing mechanism will determine which controller is required for this and then controller will decide which Action Result method is there to serve this request.

Followings are the return types an Action Result Methods can be written after completion of the execution of business logic written in that method:

  1. View: Renders a view as a Response
  2. Redirect: It will redirect to another page or another action using its URL
  3. PartialView: Renders a partial portion of view, which will define a portion that can be renders in another view.
  4. RedirectToAction/RedirectToRoute: Redirect to another action method Content- Returns a user defined content type
  5. JSON: Returns a serialized object, which can be handled by JavaScript to render HTML and other data.
  6. File: Returns binary output as a file to write on the response.

Action Result Method with Parameters:

An Action method may or may not contain parameters. If it contains parameters, then it will be retrieved from user request or user interactions. This parameter will be key/value pairs. Typical example of ActionResult method will be the following.

public ActionResult Edit(int id)
{
    //Application Logic
    return View(); 
}

Here, we will get the Id of particular class, which we need to edit and then once business logic written is completed, we will be returning a view for that.

Views:

ASP.NET MVC Framework support use of view engine to generate a View. View is returned by an Action Result method in a response to the request. Lastly, view will be render as HTML in browser in response to user request and user interaction.

Following is a typical example of view:

@{
    ViewBag.Title = "Home Page";
}

<h2>@ViewBag.Message</h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>

As we are using razor syntax for this whole series, it will render a view bag message in the browser, which will be written in Index Action Result methods above.

Partial Views:

A partial view will allow rendering a portion of view that will be rendered inside a parent view. Typical example of partial view will be a breadcrumb, which will be rendered as part of parent view.

Models:

As now, we have discussed View, Controller and Action Result methods, It’s now time to request Model. A model contains all the classes and application logic that are not there in your view and controllers. Model classes contain all business logics, data access and validation logic.

A view should contain only logic to display UI as response. A controller should contain minimum logic to return respective view with action result methods and all other logic will be done in the Model classes. Suppose you are using Linq-To-SQL or Entity framework to access your database you should created related to classes in this Model folder.

A typical example of Model will be following:

public class LogOnModel
{
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }
}

In the above example, you can see the Logon Model classes, which contains three properties UserName, Password and Remember Me. In addition, you can see the different attribute like required, which will say this field is a required field to controller classes. So as written in above example, it contains whole validation logic, database access logic etc.

Therefore, that’s it. In next chapter, we will learn about different type of views.


Jalpesh
15 · 11% · 3478
6



Submit

Your Comment


Sign Up or Login to post a comment.

"Getting Started with ASP.NET MVC - Part 3: Controller basics, Action Results, Models and View" rated 5 out of 5 by 6 readers
Getting Started with ASP.NET MVC - Part 3: Controller basics, Action Results, Models and View , 5.0 out of 5 based on 6 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]